Re: [xslt] Extracting just the value of a property



On Sun, 2008-02-24 at 19:39 +0000, Dan Stromberg wrote:
> Say you have an xml file like:
> 
> <project name="FullDist" default="full">
>     <property name="projectname" value="Bleeding_Edge"/>
>     <!-- Lots of other stuff here -->
> </project>
> 
> And you want to pull out just Bleeding_Edge without a lot of xml framing.
> 
> >From a shell script, is xsltproc a good way of doing this?  If so how?

I wrote a simple C program that takes an XML document and an XPath
expression, and returns the result.  You're welcome to a copy.

Here it would be, xpath '/project/property[ name="projectname"]/@value'

> Would xquery be better than XSLT?

If your document is large, or you're doing it often, yes, probably,
because then you could have the values indexed, and also because
you have access to a lot more functions you can call in your
XPath expressions if you need them.

As a side-note, this XML design assumes that neither names nor
properties ever contain markup themselves, and are always in the
same language.  If you used sub-elements, you could avoid these
constraints, e.g.

<project>
  <property>
    <name>The Equation</name>
    <value>E = mc<sup>2</vaue>
  </property>
  <property>
    <name xml:lang="en">trousers</name>
    <name xml:lang="en_US">pants</name>
    <name xml:lang="fr">calsons</name>
    <value>0</value>
  </property>
</project>

Liam

-- 
Liam Quin - XML Activity Lead, W3C, http://www.w3.org/People/Quin/
Pictures from old books: http://fromoldbooks.org/
Ankh: irc.sorcery.net irc.gnome.org www.advogato.org
> 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]