Parent Branch Selection

Here should go questions about transforming XML with XSLT and FOP.
fcsonline
Posts: 3
Joined: Wed Nov 08, 2006 4:44 pm

Parent Branch Selection

Post by fcsonline »

Hello I have this problem.

My source XML is:

Code: Select all


<?xml version="1.0"?>
<root>
<folder name="pack1" other="unknownattributes">
<file name="file1" type="active"/>
<file name="file2" type="active"/>
<file name="file3" type="active"/>
<file name="file4" type="inactive"/>
<file name="file5" type="inactive"/>
</folder>
<folder name="pack2" other="unknownattributes">
<file name="file11" type="active"/>
<file name="file21" type="inactive"/>
<file name="file31" type="active"/>
<file name="file41" type="active"/>
<file name="file51" type="active"/>
</folder>
<folder name="pack3" other="unknownattributes">
<file name="file13" type="active"/>
</folder>
</root>
I want to filter by inactive files, but I want the same tree structure. I want this output:

Code: Select all


<?xml version="1.0"?>
<root>
<folder name="pack1" other="unknownattributes">
<file name="file4" type="inactive"/>
<file name="file5" type="inactive"/>
</folder>
<folder name="pack2" other="unknownattributes">
<file name="file21" type="inactive"/>
</folder>
</root>
I'm trying with XSLT like this:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:copy-of select="//folder/file[@type='inactive']/parent::folder/parent::root" />
</xsl:template>
</xsl:stylesheet>
But when I put the parent axe it selects all the node and I only want the direct parent, the branch.
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Start with a default copy template and add a another template that maches elements. Then check if the element is an inactive file or if that element contains an inactive file and only then copy the element to the outout. Full example below

Code: Select all


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*" priority="10">
<xsl:if test="self::file[@type='inactive'] or .//file[@type='inactive']">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:if>
</xsl:template>

</xsl:stylesheet>
Best Regards,
George
George Cristian Bina
Post Reply