开发者

Xslt recursive function that return linked tree

This is a XSLT code:

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="*[parent::*]">
  <xsl:param name="pPath"/>
  <xsl:value-of select="$pPath"/>
  <xsl:variable name="vValue" select="normalize-space(text()[1])"/>
  <xsl:value-of select="$vValue"/>
  <br/>
  <xsl:apply-templates select="*">
    <xsl:with-param name="pPath" select="concat($pPath, $vValue, ': ')"/>
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="text()"/>

when appliedon this开发者_如何转开发 XML:

<ItemSet>
<Item>1
 <iteml1>1.1</iteml1>
 <iteml1>1.2</iteml1>
</Item>
<Item>2
  <iteml1>2.1
    <iteml2>2.1.1</iteml2>
   </iteml1>
</Item>
</ItemSet> 

the result is :

1
1: 1.1
1: 1.2
2
2: 2.1
2: 2.1: 2.1.1

what should i add to it so i can have a link on every elemet of that tree as example :

<a href="1">1</a>
<a href="1">1</a> : <a href="1/1.1">1.1</a>
<a href="1">1</a> : <a href="1/1.2">1.2</a>
<a href="2">2</a>
<a href="2">2</a> : <a href="2/2.1">2.1</a>
<a href="2">2</a> : <a href="2/2.1">2.1</a> : <a href="2/2.1/2.1.1">2.1.1</a>

etc...


<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output omit-xml-declaration="yes"/>
        <xsl:template match="*[parent::*]">
                <xsl:param name="pPath"/>
                <xsl:value-of select="$pPath"/>
                <xsl:variable name="vValue" select="normalize-space(text()[1])"/>
                <a href="{$vValue}">
                        <xsl:value-of select="$vValue"/>
                </a>
                <br/>
                <xsl:apply-templates select="*">
                        <xsl:with-param name="pPath" select="concat($pPath, $vValue, ': ')"/>
                </xsl:apply-templates>
        </xsl:template>

        <xsl:template match="text()"/>
</xsl:stylesheet>

or try this one

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output omit-xml-declaration="yes" method="html"/>
        <xsl:template match="ItemSet/Item">
        <xsl:variable name="vValue" select="normalize-space(text()[1])"/>
                <a href="{$vValue}">
                        <xsl:value-of select="$vValue"/>
                </a>
                <br/>
                <xsl:apply-templates select="*" />
        </xsl:template>

        <xsl:template match="iteml1 | iteml2">
                <xsl:variable name="vValue" select="normalize-space(text()[1])"/>
                <xsl:variable name="vValueparent" select="normalize-space(../text()[1])"/>

                <a href="{$vValueparent}">
                        <xsl:value-of select="$vValueparent"/>
                </a> :
                <a href="{$vValue}">
                        <xsl:value-of select="$vValue"/>
                </a><br/>
                <xsl:apply-templates select="*"/>
        </xsl:template>        
</xsl:stylesheet>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜