Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 10th, 2013, 08:43 AM
Registered User
 
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?
 
Old September 10th, 2013, 09:06 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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?
 
Old September 10th, 2013, 09:11 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
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
 
Old September 10th, 2013, 10:17 AM
Registered User
 
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.
 
Old September 10th, 2013, 11:32 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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?





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.