LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-01-2013, 09:25 AM   #1
slapshot136
LQ Newbie
 
Registered: Mar 2013
Posts: 4

Rep: Reputation: Disabled
Using variable instead of files as input for xsltproc


I would like to use 2 variables rather than files as input for xsltproc, is this possiblle?

something like:

Code:
xml="<?xml... />"
xslt="<?xml transform.. />"
result=`xsltproc $xslt $xml`
P.S. I am trying to avoid writing to the disk (as this may kill the flash disk that I am using), and will be using this in a bash script
 
Old 03-01-2013, 09:43 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
I cannnot see any problem with it. So yes, you can.

But explain your concerns little more... What exactly your requirement is and where are you stuck?

Last edited by shivaa; 03-01-2013 at 09:44 AM.
 
Old 03-01-2013, 10:06 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,059

Rep: Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266
Try Process Substitution:

Code:
xml="<?xml... />"
xslt="<?xml transform.. />"
result=$(xsltproc <(echo "$xslt") <(echo "$xml"))
 
1 members found this post helpful.
Old 03-01-2013, 10:46 AM   #4
slapshot136
LQ Newbie
 
Registered: Mar 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shivaa View Post
I cannnot see any problem with it. So yes, you can.

But explain your concerns little more... What exactly your requirement is and where are you stuck?
requirement: run xsltproc in script without using any external files

problem: xsltproc takes 2 files as input


what I currently have:

Code:
echo "$xml" > xml.xml
echo "$xslt" > xslt.xslt
xsltproc xslt.xslt xml.xml # this works but uses the files
xsltproc <(echo "$xslt") <(echo "$xml") # this does not work, gives parser error: Document is empty
Quote:
Originally Posted by ntubski View Post
Try Process Substitution:

Code:
xml="<?xml... />"
xslt="<?xml transform.. />"
result=$(xsltproc <(echo "$xslt") <(echo "$xml"))
Code:
result=$(xsltproc <(echo "$xslt") <(echo "$xml"))
does not work, same error - but the Process Substitution seems to be what I need, thanks

edit: using
Code:
set +o posix
changed the error to parser error : Start tag expected, '<' not found, but the variable data is valid (it's unchanged from before)

P.S. what is the difference between:
Code:
xsltproc xslt.xslt xml.xml # this works
xsltproc <(cat xslt.xslt) <(cat xml.xml) # this does not work?
edit#2:
this was helpful

Code:
 echo "$xslt" | xsltproc - xml.xml
echo "$xml" | xsltproc xslt.xslt -
the above works and takes care of 1 of the file inputs, but is there any way of using it to do both?

Last edited by slapshot136; 03-01-2013 at 02:38 PM.
 
Old 03-01-2013, 03:46 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,059

Rep: Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266
Quote:
Originally Posted by slapshot136 View Post
Code:
result=$(xsltproc <(echo "$xslt") <(echo "$xml"))
does not work, same error - but the Process Substitution seems to be what I need, thanks
Can you show exactly what doesn't work (also OS+shell version)? This works for me:

Code:
#!/bin/bash
# xsltproc.sh
xml='<?xml version="1.0"?>
<x/>'

xslt='<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/x">
    <y/>
  </xsl:template>
</xsl:stylesheet>'

xsltproc <(echo "$xslt") <(echo "$xml")
Code:
~/tmp$ ./xsltproc.sh 
<?xml version="1.0"?>
<y/>
~/tmp$ bash --version
GNU bash, version 4.2.37(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
~/tmp$ uname -somr
Linux 3.2.0-4-amd64 x86_64 GNU/Linux
Code:
xsltproc xslt.xslt xml.xml # this works
xsltproc <(cat xslt.xslt) <(cat xml.xml) # this does not work?
Those should work the same, but apparently Process Substitution is not working for you.
 
1 members found this post helpful.
Old 03-01-2013, 04:03 PM   #6
slapshot136
LQ Newbie
 
Registered: Mar 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
Can you show exactly what doesn't work (also OS+shell version)?
Red Hat Enterprise Linux Server release 5.8 (Tikanga)

GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.

Linux 2.6.18-308.4.1.el5 x86_64 GNU/Linux

Using libxml 20626, libxslt 10117 and libexslt 813
xsltproc was compiled against libxml 20626, libxslt 10117 and libexslt 813
libxslt 10117 was compiled against libxml 20626
libexslt 813 was compiled against libxml 20626


Code:
[user]$ xml='<?xml version="1.0"?>
> <x/>'
[user]$ xslt='<?xml version="1.0"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
>   <xsl:template match="/x">
>     <y/>
>   </xsl:template>
> </xsl:stylesheet>'
[user]$ echo "$xml" > xml.xml
[user]$ echo "$xslt" > xslt.xslt
[user]$ cat xslt.xslt
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/x">
    <y/>
  </xsl:template>
</xsl:stylesheet>
[user]$ cat xml.xml
<?xml version="1.0"?>
<x/>
[user]$ xsltproc <(echo "$xslt") <(echo "$xml")
/dev/fd/63:1: parser error : Document is empty

^
/dev/fd/63:1: parser error : Start tag expected, '<' not found

^
cannot parse /dev/fd/63
[user]$ xsltproc xslt.xslt xml.xml
<?xml version="1.0"?>
<y/>
[user]$diff <(cat xslt.xslt) <(cat xml.xml)
2,6c2
< <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<   <xsl:template match="/x">
<     <y/>
<   </xsl:template>
< </xsl:stylesheet>
---
> <x/>
[user]$ diff <(echo "$xslt") <(echo "$xml")
2,6c2
< <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<   <xsl:template match="/x">
<     <y/>
<   </xsl:template>
< </xsl:stylesheet>
---
> <x/>
that is what I get when I run those commands..

Last edited by slapshot136; 03-01-2013 at 04:19 PM.
 
Old 03-01-2013, 05:43 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,059

Rep: Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266
That's weird, Process Substitution is working for diff but not xsltproc. Maybe try updating to a newer version of xsltproc? A possible workaround would be to use named fifo's:
Code:
mkfifo xpipe
echo "$xslt" > xpipe &
echo "$xml" | xsltproc xpipe -
That does require a disk write to write the directory entry for xpipe, but the data itself is not written to disk (and xpipe is reusable).

Another possibility is to use files, but put them in a ramfs/tmpfs.
 
1 members found this post helpful.
Old 03-04-2013, 08:58 AM   #8
slapshot136
LQ Newbie
 
Registered: Mar 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
I can live with the xslt being on the disk, as that one shouldn't change too often in my use case, so I will just go with

Code:
echo "$xml" | xsltproc xpipe -
thanks for your help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] variable file input lonesoac0 Linux - Newbie 4 10-02-2011 02:45 AM
problem while comparing awk field variable with input variable entered using keyboard vinay007 Programming 12 08-23-2011 01:44 AM
Piping curl and xsltproc bozox Linux - Newbie 1 07-28-2009 04:50 PM
missing xsltproc on fc4 vadiml Fedora 2 01-24-2008 07:50 AM
bash - how to take input as a variable? trees Linux - General 1 02-19-2004 05:09 PM


All times are GMT -5. The time now is 11:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration