Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 10th, 2013, 08:43 AM
Registered User
Points: 28, Level: 1
Points: 28, Level: 1 Points: 28, Level: 1 Points: 28, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2013
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default Specific order, need order from XSL not from input xml format

I am facing a problem with same name nodes at the same level and specific order. My order depends on the value of their nodes. My output should be a txt file. My XML looks like:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<header>
             <B>
		<element>
			<value>10</value>
			<value>test</value>
		</element>
	</B>

	<A>
		<element>
			<value>100</value>
			<value>message</value>
		</element>
	</A>
             <B>
		<element>
			<value>20</value>
			<value>phase</value>
		</element>
	</B>

	<A>
		<element>
			<value>101</value>
			<value>type</value>
		</element>
	</A>
             <B>
		<element>
			<value>20</value>
			<value>phase</value>
		</element>
	</B>

	<A>
		<element>
			<value>101</value>
			<value>type</value>
		</element>
	</A>
             
</header>
My txt output should look like that.

typephase
typephase


and so on.

My XSL looks like that
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	version="1.0">
	<xsl:output method="text" indent="yes" />
	<xsl:template match="header/child::node()">
			
			<xsl:if test="name(.) = 'A' and element/value='101'">
				<xsl:value-of select="element/value[position()=2]" />
			</xsl:if>
			<xsl:if test="name(.) = 'B' and element/value='20'">
				<xsl:value-of select="element/value[position()=2]" />
			</xsl:if>
			<xsl:text>&#xa;</xsl:text>		
	</xsl:template>
	
	<xsl:template match="/">
		<xsl:apply-templates select="header"></xsl:apply-templates>
	</xsl:template>
</xsl:stylesheet>
The result with this XSL is:

phasetype
phasetype


xsl:sort descending doesn`t help me, because the original input XML has much more nodes. I need the order from the XSL not from the input XML document. Does anyone know, how to solve this?
Reply With Quote
  #2 (permalink)  
Old September 10th, 2013, 09:06 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Something like the following.

Code:
<xsl:template match="header/*">
   <xsl:sort select="element/value[1]" datatype="number"/>
   ...
</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
  #3 (permalink)  
Old September 10th, 2013, 09:11 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Please try describing in English some kind of specification of your transformation, e.g. the following is consistent with your results, but I've no idea if this is what you had in mind:

First process all the elements with value 101, processing the A elements first, then the B elements.

Then process all the elements with value 20, processing the A elements first, then the B elements.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #4 (permalink)  
Old September 10th, 2013, 10:17 AM
Registered User
Points: 28, Level: 1
Points: 28, Level: 1 Points: 28, Level: 1 Points: 28, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2013
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default

The problem with both solutions are that the result would be then

type type phase phase

if i understand you correctly

but I need

type phase
type phase
type phase

<A> <B>

and so on.

normally the order output of a xsl transformation depends on the order in the xml input but i want to define the order in the xsl stylesheet.
Reply With Quote
  #5 (permalink)  
Old September 10th, 2013, 11:32 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You've described what you want the output to look like, but you need to describe, really well, how you work out that ordering.

How do you know that the first B comes after the first A, and not after the second A? Are the elements always in pairs, and you want to reverse the order (so B -> A, becomes A -> B)? Or is there some other logic in your ordering that you could make clear to us?

You say you want to define the order in the XSL stylesheet, but we really don't know what that ordering is.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Order XML elements using XSLT imshriram XSLT 9 July 10th, 2011 10:56 AM
XSLT for displaying XML data in list order kvishnu XSLT 1 May 11th, 2009 08:27 AM
Getting fields, specific order from xml using xslt Jaipal XSLT 4 July 23rd, 2007 11:35 AM
order XML file alphabetically bcogney XML 2 April 18th, 2006 01:45 PM
Order a recordset by specific values SoC SQL Language 2 August 28th, 2005 08:27 PM



All times are GMT -4. The time now is 08:13 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.