3

We are transforming some like following xml:

<collection>
    <availableLocation>NY</availableLocation>
    <cd>
        Fight for your mind
    </cd>
    <cd>
        Electric Ladyland
    </cd>
    <availableLocation>NJ</availableLocation>
</collection>

with the following xslt

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
    <html>
        <body>

                 <xsl:apply-templates select="collection/availableLocation"/>
                <xsl:apply-templates select="collection/cd"/>

        </body>
    </html>
</xsl:template>
<xsl:template match="availableLocation">
    <h3>
        <xsl:value-of select="."/>
    </h3>
</xsl:template>
<xsl:template match="cd">
    <xsl:value-of select="."/><br/>
</xsl:template>


</xsl:stylesheet>

and the output is :

NY

NJ

Fight for your mind 
Electric Ladyland 

We want to preserve order as it in xml. We want output as follows :

NY

Fight for your mind 
Electric Ladyland 

NJ

Is there any way to do this ? plz comment/suggest.

i found the solution by doing these changes

            <xsl:for-each select="collection">

                <xsl:apply-templates select="."/>

                </xsl:for-each>

    </body>

Plz let us know if there is better solution to do this.

thanks in advance

dev3786
  • 31
  • 3

2 Answers2

2
<xsl:apply-templates select="collection/availableLocation|collection/cd"/>
Peter Davis
  • 761
  • 7
  • 13
1

One of the simplest possible solutions doesn't require even an explicit <xsl:apply-templates>:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="text()">
  <xsl:value-of select="normalize-space()"/>
  <xsl:text>&#xA;</xsl:text>
 </xsl:template>
</xsl:stylesheet>

When applied on the provided XML document:

<collection>
    <availableLocation>NY</availableLocation>
    <cd>
      Fight for your mind
    </cd>
    <cd>
      Electric Ladyland
    </cd>
    <availableLocation>NJ</availableLocation>
</collection>

the wanted, correct result is produced:

NY
Fight for your mind
Electric Ladyland
NJ
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431