[XML4Lib] XPath: Selecting nodes grouped with parent?

Charles Yates ceyates at stanford.edu
Tue Oct 31 17:48:51 EST 2006

You don't say, but I am assuming you are using xpath in the context  
of an xslt transformation.

Since you want a subset of nodes, each with its own subset, you need  
to do this in two passes, ie two xpath expressions.  One to get the  
larger subset (subcategory) and one to get the desired children  
(database) of each node of that subset

<xsl:template match="subcategory[database/type='foo']">
<xsl:copy-of select="database[type='foo']"/>


On Oct 31, 2006, at 1:40 PM, Walker, David 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>
> What I want to be able to do is apply a limit to the file based on  
> one or more attributes in the databases.  For example, only showing  
> those databases that have a <type> field with a value of  
> 'Bibliography'. But I want to be able to keep them in their  
> subcategory groupings.  In essence, I want to select both children  
> (databases) and parent (subcategory) nodes using an XPath  
> expression with conditional logic based on the children.
> Is this possible?  I have a feeling the parent::axis might be able  
> help here, but I'm not sure.
> I've tried a couple of things. This returns ONLY those databases  
> that have a type of Bibliography, but obviously doesn't include the  
> subcategory parent nodes:
>   //database[type = 'Biography']
> This returns subcategory nodes that have a child database with the  
> type of Bibliography, but it also includes ALL of the child nodes,  
> regardless of whether they have a type of 'Bibliography' or not.
>   //subcategory[database/type = 'Biography']
> I'm looking to find the intersection of those two, grabing any  
> subcategory node with a child database type of Bibliography, but  
> then limit the children to also be only ones with a type of  
> Bibliogrtaphy.
> I can tackle this in other ways, but was hoping a single Xpath  
> expresison could do the trick.  Thanks!
> --Dave
> -------------------
> David Walker
> Library Web Services Manager
> California State University
> http://xerxes.calstate.edu
> _______________________________________________
> XML4Lib mailing list
> XML4Lib at webjunction.org
> http://lists.webjunction.org/mailman/listinfo/xml4lib

More information about the XML4Lib mailing list