XSL2.0将等效代码标记为XSL1.0

时间:2015-10-21 19:31:47

标签: xml xslt

XSL1.0中的以下等价物是什么?

<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text"/>

  <xsl:template match="/">

    <xsl:variable name="sampleString">XML,XSLT,XPath,SVG,XPointer</xsl:variable>

    <xsl:variable name="tokenizedSample" select="tokenize($sampleString,',')"/>

    <xsl:for-each select="$tokenizedSample">
      <xsl:value-of select="."/>
      <xsl:text>! </xsl:text>
    </xsl:for-each>

Second item in tokenizedSample: 
    {<xsl:value-of select="item-at($tokenizedSample,2)"/>}

Tenth item in tokenizedSample: 
    {<xsl:value-of select="item-at($tokenizedSample,10)"/>}

Position of SVG in tokenizedSample:
    {<xsl:value-of select="index-of($tokenizedSample,'SVG')"/>}

Position of XSL-FO in tokenizedSample:
    {<xsl:value-of select="index-of($tokenizedSample,'XSL-FO')"/>}

End of test.
  </xsl:template>

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

正如我已经建议here,你需要两次通过:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="text"/>

<xsl:variable name="sampleString">XML,XSLT,XPath,SVG,XPointer</xsl:variable>

<xsl:variable name="tokens">
    <xsl:call-template name="tokenize">
        <xsl:with-param name="text" select="$sampleString"/>
    </xsl:call-template>
</xsl:variable>

<xsl:variable name="tokens-set" select="exsl:node-set($tokens)/token" />

<xsl:template match="/">
    <xsl:for-each select="$tokens-set">
        <xsl:value-of select="."/>
        <xsl:text>! </xsl:text>
    </xsl:for-each>

Second item in tokenizedSample: 
    {<xsl:value-of select="$tokens-set[2]"/>}

Tenth item in tokenizedSample: 
    {<xsl:value-of select="$tokens-set[10]"/>}

Position of SVG in tokenizedSample:
    <xsl:variable name="svg" select="$tokens-set[.='SVG']" />
    <xsl:choose>
        <xsl:when test="$svg">
            {<xsl:value-of select="count($svg/preceding-sibling::token) + 1"/>}
        </xsl:when>
        <xsl:otherwise>
            {0}
        </xsl:otherwise>
    </xsl:choose>

Position of XSL-FO in tokenizedSample:
    <xsl:variable name="fo" select="$tokens-set[.='XSL-FO']" />
    <xsl:choose>
        <xsl:when test="$fo">
            {<xsl:value-of select="count($fo/preceding-sibling::token) + 1"/>}
        </xsl:when>
        <xsl:otherwise>
            {0}
        </xsl:otherwise>
    </xsl:choose>

End of test.
</xsl:template>


<xsl:template name="tokenize">
    <xsl:param name="text" select="."/>
    <xsl:param name="separator" select="','"/>
    <token>
        <xsl:value-of select="substring-before(concat($text, $separator), $separator)" />
    </token>
    <xsl:if test="contains($text, $separator)">
        <xsl:call-template name="tokenize">
            <xsl:with-param name="text" select="substring-after($text, $separator)"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

这是一个稍微高效的实现,可以在预期&#34;位置&#34;查询:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="text"/>

<xsl:variable name="sampleString">XML,XSLT,XPath,SVG,XPointer</xsl:variable>

<xsl:variable name="tokens">
    <xsl:call-template name="tokenize">
        <xsl:with-param name="text" select="$sampleString"/>
    </xsl:call-template>
</xsl:variable>

<xsl:variable name="tokens-set" select="exsl:node-set($tokens)/token" />

<xsl:template match="/">
    <xsl:for-each select="$tokens-set">
        <xsl:value-of select="."/>
        <xsl:text>! </xsl:text>
    </xsl:for-each>

Second item in tokenizedSample: 
    {<xsl:value-of select="$tokens-set[2]"/>}

Tenth item in tokenizedSample: 
    {<xsl:value-of select="$tokens-set[10]"/>}

Position of SVG in tokenizedSample:
    {<xsl:value-of select="$tokens-set[.='SVG']/@index"/>}

Position of XSL-FO in tokenizedSample:
    {<xsl:value-of select="$tokens-set[.='XSL-FO']/@index"/>}

End of test.
</xsl:template>


<xsl:template name="tokenize">
    <xsl:param name="text" select="."/>
    <xsl:param name="separator" select="','"/>
    <xsl:param name="i" select="1"/>
    <token index="{$i}">
        <xsl:value-of select="substring-before(concat($text, $separator), $separator)" />
    </token>
    <xsl:if test="contains($text, $separator)">
        <xsl:call-template name="tokenize">
            <xsl:with-param name="text" select="substring-after($text, $separator)"/>
            <xsl:with-param name="i" select="$i + 1"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>