开发者

How to preserve xml element's data position with xslt

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


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜