[XML4Lib] XPath: Selecting nodes grouped with parent?
Erik Hetzner
erik.hetzner at ucop.edu
Tue Oct 31 17:40:08 EST 2006
At Tue, 31 Oct 2006 13:40:35 -0800,
"Walker, David" <dwalker at calstate.edu> wrote:
>
> Hi all,
>
> I have a simple set of XML files that include category and database information for various library subscription databases. Each category has one or more subcategories, and each subcategory has one or more databases, as such:
>
> <category>
> <subcategory>
> <database />
> <database />
> </subcategory>
> <subcategory>
> <database />
> <database />
> </subcategory>
> </category>
>
[…]
Hi David,
I’m pretty sure that you won’t be able to do this in pure xpath, at
least with xpath 1.0. You seem to want to build a custom node tree,
and I don’t think that xpath will let you do this. But that’s what
XSLT is for: building node trees. I think this will do what you’re
looking for.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="foo">
<xsl:for-each select="//subcategory[database[@type='1']]">
<xsl:element name="subcategory">
<xsl:attribute name="name">
<xsl:value-of select="@name"/>
</xsl:attribute>
<xsl:for-each select="database[@type='1']">
<xsl:element name="database">
<xsl:attribute name="type">
<xsl:value-of select="@type"/>
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:variable>
<xsl:copy-of select="$foo"/>
</xsl:template>
</xsl:transform>
best,
Erik Hetzner--
Erik Hetzner
California Digital Library
510-987-0884
More information about the XML4Lib
mailing list