xml interpretor in batch mode

M

M

Hello,

I'm looking for a batch mode software which can return the content of
an XML tag, if it gets the different level tags (root, level1, level2).
For Unix/Linux.

Exemple :
xml_interpretor myfile.xml root level1 level2
this_is_content_of_level2_tag

Thanx a lot !
M
 
P

Patrick TJ McPhee

% Exemple :
% > xml_interpretor myfile.xml root level1 level2
% > this_is_content_of_level2_tag

I don't understand what you're trying to do here. If the
goal is to specify a node name, or perhaps the path to
the node, and have the contents of the node returned,
what you really want is a program which can execute
XPath queries. Assuming your document looks something
like this

<root>
<level1>
<level2>data</level2>
</level1>
</root>

the XPath query would be
/root/level1/level2

I don't actually know a tool which will do that, but I do know that
you can do it with this XSLT stylesheet

<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
version='1.0'>
<xsl:eek:utput method='text'/>
<xsl:template match='/'>
<xsl:value-of select='/root/level1/level2'/>
</xsl:template>
</xsl:stylesheet>

and there are a few command-line xslt processors which can process it.
The problem can be solved by writing a program to generate that stylesheet
based on command-line arguments.

It turns out that xsltproc, the XSLT processor which comes with libxslt,
can read a stylesheet from the command-line, so you can create a shell
script which puts that stylesheet in a HERE document, with the
XPath expression taken from $2. Here's a shell script that does this.
Remove the leading space, stick it in a file in your path
and give it execute permission.

Usage is
findnode docname xpath-expression


#!/bin/sh

if [ $# -ne 2 ]
then
echo "usage $0 doc expression"
exit 1
fi

xsltproc - "$1" <<HERE

<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
version='1.0'>
<xsl:eek:utput method='text'/>
<xsl:template match='/'>
<xsl:value-of select='$2'/>
</xsl:template>
</xsl:stylesheet>
HERE
 
P

Patrick TJ McPhee

[if you'll pardon some next-day editing]

% It turns out that xsltproc, the XSLT processor which comes with libxslt,
% can read a stylesheet from the command-line, so you can create a shell

That should be `from the standard input'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top