[XML4Lib] XPath: Selecting nodes grouped with parent?

Walker, David dwalker at calstate.edu
Wed Nov 1 08:57:37 EST 2006


Thanks to those who responded, it was very helpful.
 
In this case, I'm actually not using XSLT, but rather loading the file into a DOM object, passing it with the query to an XPath object, and then walking thru the resulting node list in order to populate some arrays.
 
 
>> You seem to want to build a custom node 
>> tree, and I don't think that xpath will 
>> let you do this. 
 
I think this is right.  I had hopes of somehow limiting the node tree based on the query alone, but I think I was putting ideas of a relational database result set onto XML.
 
So I'm going to take the two pass approach, although again outside of XSLT.  Thanks!
 
--Dave
 
 
-------------------
David Walker
Library Web Services Manager
California State University
http://xerxes.calstate.edu

________________________________

From: Erik Hetzner [mailto:erik.hetzner at ucop.edu]
Sent: Tue 10/31/2006 2:40 PM
To: Walker, David
Cc: xml4lib at webjunction.org
Subject: Re: [XML4Lib] XPath: Selecting nodes grouped with parent?



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