XML/XSL if else子字符串



如果条目太长,我想在XML/XSL中用if子句对变量进行子串运算。

我试过这样的东西,但效果不是那样的。

<xsl:variable id="newId" select="./newId"/>
<xsl:template match="newId">
<xsl:choose>
<xsl:when test="string-length() &lt; 15">
<xsl:value-of select="newId"/>  
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring(.,1,15)" />
<br>
<xsl:value-of select="substring(.,16)" />
</br>
</xsl:otherwise>
</xsl:choose>

代码中有一些内容需要更改。

xsl:variable具有"name"属性,而不是"id"
  • 字符串长度函数需要一个参数
  • 以下是我的操作方法:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
    <xsl:variable name="newId" select="'SomeText123456789'"/>
    <xsl:choose>
    <xsl:when test="string-length($newId) &lt; 15">
    <xsl:value-of select="$newId"/>  
    </xsl:when>
    <xsl:otherwise>
    <xsl:value-of select="substring($newId,1,15)" />
    <br>
    <xsl:value-of select="substring($newId,16)" />
    </br>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:template>
    
    </xsl:stylesheet>
    

    请在此处查看它的工作情况:https://xsltfiddle.liberty-development.net/jxDjind

    我没有发现你的模板有什么特别的错误,除了:

    <xsl:value-of select="newId"/>  
    

    需要:

    <xsl:value-of select="."/>  
    

    因为您已经处于CCD_ 1的上下文中。

    如果你不使用这个变量,或者根本不使用它,就不要明白为什么你需要这个变量。如果你出于某种原因确实需要它,那么就正确地定义它;你现在所拥有的将会产生一个错误。

    最新更新