[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: passing filtered tree to template

Subject: Re: passing filtered tree to template
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Mon, 06 Jun 2011 18:11:44 +0200
Re:  passing filtered tree to template
Hi Stefan,

The easy way out here is to add the filter to the matching template instruction, and add another matching template for those that don't match, or, if all "item" elements need to be processed in the same way, but item with property=x need to be processed with an extra instruction, something like this should work:

<xsl:template match="items">
    <xsl:apply-templates select="item" />
</xsl:template>

<!-- called ONLY when property X is true -->
<xsl:template match="item[property='x']">
    <myItem>
        <xsl:value-of select="index"/>
    </myItem>
</xsl:template>

<!-- called for all other situations (may leave empty if you don't want to process them) -->
<xsl:template match="item">
    <otherItem>
        <xsl:value-of select="index"/>
    </otherItem>
</xsl:template>

I have to note that I found your instruction a tad confusing, so if I misunderstood, please try to clarify with a simplified input/output example of XML.


Kind regards,


Abel Braaksma



On 6-6-2011 17:56, Stefan Hunziker wrote:
Hi
I have the following problem: Given an xml like the following:

<test>
     <items>
         <item>
             <index>1</index>
             <property>x</property>
         </item>
         <item>
             <index>2</index>
             <property>y</property>
         </item>
         <item>
             <index>3</index>
             <property>x</property>
         </item>
     </items>
</test>


and a stylesheet like:



<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="test"> <myTest> <xsl:variable name="xItems"> <xsl:copy-of select="items"/> </xsl:variable> <xsl:apply-templates select="items[item/property='y']"/><!-- wrong try! --> </myTest> </xsl:template>

     <xsl:template match="items">
         <xsl:for-each select="item">
             <myItem>
                 <xsl:value-of select="index"/>
             </myItem>
         </xsl:for-each>
     </xsl:template>
</xsl:stylesheet>



I would like to pass only the items with property='x' to the items
matching template. The first try as written above of course doesn't
work, if there is an x item then the whole tree is passed, if there is
none, nothing is passed. I do further processing in the items
template, so I don't want to change it to match="item" or to apply the
filter in the items template.

Any help is much appreciated!

thanks, kind regards

Stefan

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.