Jump to content

xsl:sort not working


msalamon

Recommended Posts

I am working with a rather ungainly XML document that I need to transform but that I cannot transform in any way before it gets to my stylesheet. In this XML document, I need to sort the <document> nodes based on the value of one of the <field @x_count> nodes. The format of the XML document is as follows:<documentList> <document> <content> <field name="field1" order="1">abc</field> <field name="x" order="2">def</field> <field name="x" order="3">ghi</field> <field name="x" order="4">GOOD</field> <field name="x_count" order="5">4</field> <field name="x_count" order="6">5</field> <field name="x_count" order="7">1</field> </content> </document> <document> <content> <field name="field1" order="1">xyz</field> <field name="x" order="2">GOOD</field> <field name="x" order="3">ghi</field> <field name="x" order="4">dfdd</field> <field name="x" order="5">dfddf</field> <field name="x_count" order="6">3</field> <field name="x_count" order="7">4</field> <field name="x_count" order="8">5</field> <field name="x_count" order="9">1</field> </content> </document></documentList>The important things to note: * Every <field> node has an @order attribute, which is sequential inside each <document>. So the first <field> node in a <document> has @order="1", the 2nd @order="2", etc. The @order values restart in each document. * Every document has zero to N <field> nodes with @x attribute * Every document has zero to N <field> nodes with @x_count attribute and the number of <field> nodes with an @x attribute equals the number of <field> nodes with an @x_count attribute. The @x and @x_count attributes are paired togetherI need to sort the documents in descending order based on the text value of the <field> node with an @x_count attribute, where the @x_count attribute matches the <field> node with an @x attribute where the text value of that <field> is GOOD. In the example above, the first document has a <field/@x> = GOOD where the @order = 4, and this is the 3rd <field/@x>. That corresponds to the 3rd <field/@x_count>, which has an @order = 7, with a text value of 1. (@order = 7 is the sum of the total number of <field> nodes with an @x attribute (3) and the value of the @order attribute for the <field @x> equal to GOOD (4): 3 + 4 = 7.)The second document has a <field/@x> = GOOD where the @order = 2, and this is the 1st <field/@x>. That corresponds to the 1st <field/@x_count>, which has an @order = 6, with a text value of 3. I want to sort on the 1 and 3 values, in descending order. So the 2nd document would be sorted before the 1st.Here is my XSLT (from the context of <documentList>):<xsl:for-each select="document"> <xsl:sort order="descending" data-type="number" select="content/field[@name=x_count][@order = format-number( (count(content/field[@name=x])) + number(content/field[@name=x][ normalize-space(.) = 'GOOD']/@order),'0' )]"/></xsl:for-each>(Btw, I have tried this with both the format-number and without, with the same results.)This should work, but does not. Basically, I am trying to grab the <field> node with an @x_count attribute where the @order attribute is the sum of total number of <field> nodes with an @x attribute and the value of the @order attribute for the <field @x> that is equal to GOOD.If I simply try to output the value of:format-number( (count(content/field[@name=x])) + number(content/field[@name=x][ normalize-space(.) = 'GOOD']/@order),'0' )I do get the correct value.if I try to sort by hardcoding the value of @order: <xsl:sort order="descending" data-type="number" select="content/field[@name=x_count][@order = '3']"/>that works, but without the correct result (b/c the value of @order changes in each <document>).For some reason, when I marry the two parts together, no value is returned by the XSLT.(In fact, my XSLT is a bit more complicated, b/c the value 'GOOD' is not hardcoded but is based on another node in the XML, but that comes from a fixed location.)

Link to comment
Share on other sites

got the answer elsewhere: content/field as the child of field does not work. To fix it make the sort:<xsl:sort order="descending" data-type="number" select="content/field[@name=x_count][@order = count(../field[@name=x])) + number(../field[@name=x][ normalize-space(.) = 'GOOD']/@order]"/>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...