xsl-list
[Top] [All Lists]

RE: Filter nodelist

2002-11-27 14:11:52
Ok so this is a few weeks over old. 

Assuming that a product node is current, I'd use:

count(preceding-sibling::product[(_at_)id=current()/@id])

That is, I took the following XML:
<products>
        <product id="A"/>
        <product id="B"/>
        <product id="C"/>
        <product id="B"/>
        <product id="B"/>
        <product id="D"/>
        <product id="E"/>
</products>

And applied the following transform (essentially the identity transform
with this calculation added to the product)
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
        <xsl:template match="node()|@*">
                <xsl:copy>
                        <xsl:apply-templates select="*|@*"/>
                </xsl:copy>
        </xsl:template>

        <xsl:template match="product">
                <xsl:copy>
                        <xsl:attribute name="previous_same_id_count">
                                <xsl:value-of
select="count(preceding-sibling::product[(_at_)id=current()/@id])"/>
                        </xsl:attribute>
                        <xsl:apply-templates select="*|@*"/>
                </xsl:copy>
        </xsl:template>
</xsl:stylesheet>


And got the following result (running from within Stylus Studio)

<?xml version='1.0' ?>
<products>
  <product id="A" previous_same_id_count="0"/>
  <product id="B" previous_same_id_count="0"/>
  <product id="C" previous_same_id_count="0"/>
  <product id="B" previous_same_id_count="1"/>
  <product id="B" previous_same_id_count="2"/>
  <product id="D" previous_same_id_count="0"/>
  <product id="E" previous_same_id_count="0"/>
</products>

Which should be the thing you are looking for. 

Adam




-----Original Message-----
From: owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com 
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com] On Behalf Of 
Martin Kupisch
Sent: November 7, 2002 3:11 AM
To: XSL-List(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Filter nodelist


Hello everyone,

I have an XML document that contains a few 'product' elements 
with 'id' attributes. Because I have to operate on these 
products a few times I build a variable that just takes all 
products with the expression 'descendant::product'. Sometimes 
I have to iterate over all products. But that is not all I do 
with the list. My problem: I have to get the count of all 
products from this node-list that have the same id like a 
certain product in the list  and that's position in the 
node-list is lower. Example:

[product id="A",
 product id="B",
 product id="C",
 product id="B",
 product id="B",  <-
 product id="D",
 product id="E"]

So if I have the product at position 5 the result of the 
count should be 2, because of the products at position 2 and 
4 with the same id.

Any ideas? Thanks.


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>