XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 18th, 2004, 05:04 AM
jerry.gadd jerry.gadd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 3 jerry.gadd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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?

Reply With Quote
  #2  
Old June 18th, 2004, 10:38 AM
sherzy sherzy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 1 sherzy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #3  
Old June 21st, 2004, 03:32 AM
jerry.gadd jerry.gadd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 3 jerry.gadd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #4  
Old June 22nd, 2004, 10:41 AM
jerry.gadd jerry.gadd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 3 jerry.gadd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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="userrocessAttribute(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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > XSL:SORT by processed attributes value


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT