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 August 29th, 2013, 12:45 AM
Registered User
 
Join Date: Aug 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sorting elements accessed via apply-templates based on the real XML tag name in XSLT

Hi I have got a XML like this:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<root xmlns:xlink="http://www.w3.org/1999/xlink"  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"  xmlns:skos="http://www.w3.org/2004/02/skos/core#"  version="2.8.1"  vocab-version="2013-02-28" >
  <section1>
    <a xml:id="a100">
      <ref xlink:href="#aXXX"/>
      <ref xlink:href="#aYYY"/>
    </a>
  </section1>
  <section2>
    <b xml:id="aXXX">
      DataB
    </b>
    <c xml:id="aYYY">
      DataC
    </c>
    </section2>
</root>
I want to apply the following XSLT:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:db="http://docbook.org/ns/docbook"
	xmlns:xlink="http://www.w3.org/1999/xlink">
	<xsl:output method="text"  indent="yes"/>
	<xsl:strip-space elements="*" />
	<xsl:preserve-space elements="db:para db:literallayout" />
	<xsl:param name="code" />
	<xsl:key name='mykeys' match='/root/section2/*' use='concat("#", @xml:id)'/>
 
	<xsl:template match="/" >
		<xsl:apply-templates select='/root/section1/a/ref'>
		</xsl:apply-templates>
	</xsl:template>
 
	<xsl:template match="ref">
		<xsl:apply-templates select='key("mykeys", @xlink:href)' >
		</xsl:apply-templates>
	</xsl:template>
 
	<xsl:template match='b | c' >
		<xsl:value-of select="."  />
	</xsl:template>
</xsl:stylesheet>
I want to have the output something like this :

Code:
DataC
DataB
Notice that c’s text should be before b’s text

So I think a xsl:sort needs be added to apply-templates , something like this:

Code:
<xsl:sort select='/root/section2/*[concat("#", @xml:id)=current()/@xlink:href][local-name()]'  order="descending" />
i.e.:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:db="http://docbook.org/ns/docbook"
	xmlns:xlink="http://www.w3.org/1999/xlink">
	<xsl:output method="text"  indent="yes"/>
	<xsl:strip-space elements="*" />
	<xsl:preserve-space elements="db:para db:literallayout" />
	<xsl:param name="code" />
	<xsl:key name='mykeys' match='/root/section2/*' use='concat("#", @xml:id)'/>
 
	<xsl:template match="/" >
		<xsl:apply-templates select='/root/section1/a/ref'>
		</xsl:apply-templates>
	</xsl:template>
 
	<xsl:template match="ref">
		<xsl:apply-templates select='key("mykeys", @xlink:href)'  >
			<xsl:sort select='/root/section2/*[concat("#", @xml:id)=current()/@xlink:href][local-name()]'  order="descending" />
		</xsl:apply-templates>
	</xsl:template>
 
	<xsl:template match='b | c' >
		<xsl:value-of select="."  />
	</xsl:template>
	
</xsl:stylesheet>
But it doesn’t work. 

Notice that the ordering can’t be based on xml:id as it can be anything and it also cannot be based on the text (i.e. datac , datab , …) as they can be anything so the ordering should be based on Element Name ( i.e. <b> , <c> ) only.

I tried to understand what the actual value of select='/root/section2/*[concat("#", @xml:id)=current()/@xlink:href][local-name()]' is? so I sent it to the template as parameter:

Code:
 <xsl:template match="a">
   <xsl:apply-templates select='key("mykeys", @xlink:href)'  >
	<xsl:with-param name='testparameter' select='/root/section2/*[concat("#", @xml:id)=current()/@xlink:href][local-name()]'  />
   </xsl:apply-templates>
 </xsl:template>

<xsl:template match='b | c' >
    <xsl:param name='testparameter' />
    <xsl:value-of select='$testparameter' />
</xsl:template>
And surprising it was not the name of the element , if was the text of each element (i.e. DataB and DataC rather than b and c ) and even the order-type(descending) has not been applied as well.

Can you please help me and let me know how I can get an output like this:

Code:
DataC
DataB
Please notice that datac and datab should be accessible only via element <a> and it means I can’t use

Code:
apply-templates select='/root/section2/b' or apply-templates select='/root/section2/c'
Thanks,
 
Old August 29th, 2013, 05:28 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I think you need to put the sorting into a different template:
Code:
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:db="http://docbook.org/ns/docbook"
    xmlns:xlink="http://www.w3.org/1999/xlink">
    <xsl:output method="text"  indent="yes"/>
    <xsl:strip-space elements="*" />
    <xsl:preserve-space elements="db:para db:literallayout" />
    <xsl:param name="code" />
    <xsl:key name='mykeys' match='/root/section2/*' use='concat("#", @xml:id)'/>
 
    <xsl:template match="/" >
        <xsl:apply-templates select='key("mykeys", /root/section1/a/ref/@xlink:href)'>
                    <xsl:sort select="local-name()" data-type="text" order="descending"/>
        </xsl:apply-templates>
    </xsl:template>
 
    <xsl:template match='b | c' >
        <xsl:value-of select="."  />
    </xsl:template>
</xsl:stylesheet>
Otherwise you process single 'ref' elements in the order they occur in the input XML and that way the sorting of single referenced elements does not do anything.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old August 29th, 2013, 07:56 PM
Registered User
 
Join Date: Aug 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

the solution is here:
http://stackoverflow.com/questions/1...ml-tag-name-in





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT apply-templates namespace problem mariokofler XSLT 4 March 11th, 2010 10:27 AM
Exclude Elements in Apply Templates mail4kaja XSLT 18 November 29th, 2008 12:09 PM
simple XSLT apply-templates question fannus XSLT 7 March 27th, 2007 02:11 AM
Can I 'apply-templates' across multiple XML docume mphare XSLT 1 June 8th, 2006 05:12 PM
xsl:apply-templates on Variable containing xml nexus5 XSLT 9 November 4th, 2004 02:55 PM





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