|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
XSL:SORT by processed attributes value
Hi Guys,
I want to sort on an attribute, but after I have processed its value. Here is the code I wanted to work Code:
<xsl:for-each select="//z:row"> <xsl:variable name="Source"> <xsl:choose> <xsl:when test="@source != ''"> <xsl:value-of select="@source"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="DefaultSource"/> </xsl:otherwise> </xsl:choose> <xsl:call-template name="Source"/> </xsl:variable> <xsl:sort order="descending" select="$Source"/> <xsl:call-template name="row"/> </xsl:for-each> Any Ideas? |
|
#2
|
|||
|
|||
|
I am assuming you want to sort the rows... I think this should work - change your call template to apply templates, remove the for-each:
Code:
<xsl:variable name="Source"> <xsl:choose> <xsl:when test="@source != ''"> <xsl:value-of select="@source"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="DefaultSource"/> </xsl:otherwise> </xsl:choose> <xsl:call-template name="Source"/> </xsl:variable> <xsl:apply-templates match="row"/><xsl:sort order="descending" select="$Source"/></xsl:for-each> What I am not sure of is where you care finding this source attribute - you should post an xml snippet |
|
#3
|
|||
|
|||
|
Here is a sample of the data set
Code:
- <xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema"> + <s:Schema id="RowsetSchema"> - <s:ElementType name="row" content="eltOnly" rs:CommandTimeout="30"> - <s:AttributeType name="accountno" rs:number="1" rs:nullable="true" rs:writeunknown="true"> <s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="20" /> </s:AttributeType> - <s:AttributeType name="contact" rs:number="2" rs:nullable="true" rs:writeunknown="true"> <s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="40" /> </s:AttributeType> - <s:AttributeType name="status" rs:number="3" rs:nullable="true" rs:writeunknown="true"> <s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="30" /> </s:AttributeType> - <s:AttributeType name="key4" rs:number="4" rs:nullable="true" rs:writeunknown="true"> <s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="20" /> </s:AttributeType> - <s:AttributeType name="source" rs:number="5" rs:nullable="true" rs:writeunknown="true"> <s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="20" /> </s:AttributeType> - <s:AttributeType name="uexpsal" rs:number="6" rs:nullable="true" rs:writeunknown="true"> <s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="30" /> </s:AttributeType> <s:extends type="rs:rowbase" /> </s:ElementType> </s:Schema> - <rs:data> <z:row accountno="A4011937037A$3O%0PHI" contact="Phil" status="100" key4="0" source="" uexpsal="" /> <z:row accountno="A40213624320!Z:]OSTE" contact="Craig" status="200" key4="1" source="" uexpsal="" /> <z:row accountno="A40407675939$T*71STE" contact="Stephen" status="80" source="" uexpsal="" /> <z:row accountno="A4041342519E!(RK(JAM" contact="James" status="60" source="" /> <z:row accountno="A4041441578P&P:3ECOM" contact="Paul" status="80" source="Comapny A" uexpsal="" /> <z:row accountno="A40415349161)6&E]STE" contact="Frank" status="80" key4="4" source="A Company" /> <z:row accountno="A4041534957B-E5ZJSTE" contact="Tony" status="60" key4="0" source="" uexpsal="" /> <z:row accountno="A40416345053(E%&^ALE" contact="John" status="180" source="Another Company" /> <z:row accountno="A40419430952&0[SESTE" contact="Stuart" status="100" source="" uexpsal="" /> <z:row accountno="A4042749843F*%&UYJAN" contact="Harry" status="100" source="" uexpsal="" /> <z:row accountno="A40427617522(F6#IHUR" contact="Tom" status="80 " source="" uexpsal="" /> <z:row accountno="A4051038779B)EE@XALE" contact="Colin" status="70" source="Some Company" /> <z:row accountno="A4051953184L&V#9@STE" contact="Bob" status="80" key4="1" source="" uexpsal="" /> </rs:data> </xml> I want to act on /z:row elements sorted on their attributes @source, after I have processed the attribute (replaced all blank with a default value) as suggested in the code snippet in the original post |
|
#4
|
|||
|
|||
|
I found a solution to this and more complex sort rules
I include the msxsl namespace and create a new one called user I add a 'msxsl:script' block which implements the namspace 'user', containing a javascript which processes the attribute <msxsl:script language="JScript" implements-prefix="user"> <![CDATA[ function processAttribute(value) { if (value == '') return 'DefaultValue'; return value.substr(2, value.length - 3); } ]]> </msxsl:script> I then call the javascript from within an XPath select by passing the attribute as a string <xsl:sort select="user rocessAttribute(string(@attribute))"/>This method can be used to calculate very simple or highly complex sort-keys, and passing it any combination of DOM objects, strings, numbers, bools or XPath results. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > XSL:SORT by processed attributes value |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|