LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using variable instead of files as input for xsltproc (https://www.linuxquestions.org/questions/linux-newbie-8/using-variable-instead-of-files-as-input-for-xsltproc-4175452240/)

slapshot136 03-01-2013 08:25 AM

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

shivaa 03-01-2013 08:43 AM

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?

ntubski 03-01-2013 09:06 AM

Try Process Substitution:

Code:

xml="<?xml... />"
xslt="<?xml transform.. />"
result=$(xsltproc <(echo "$xslt") <(echo "$xml"))


slapshot136 03-01-2013 09:46 AM

Quote:

Originally Posted by shivaa (Post 4902447)
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 (Post 4902466)
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?

ntubski 03-01-2013 02:46 PM

Quote:

Originally Posted by slapshot136 (Post 4902496)
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.

slapshot136 03-01-2013 03:03 PM

Quote:

Originally Posted by ntubski (Post 4902701)
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..

ntubski 03-01-2013 04:43 PM

That's weird, Process Substitution is working for diff but not xsltproc. :scratch: 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.

slapshot136 03-04-2013 07:58 AM

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.


All times are GMT -5. The time now is 05:32 PM.