XML/XSL if else 子串
XML/XSL if else substring
如果条目太长,我想在 XML/XSL 中使用 if 子句对变量进行子字符串化。
我试过类似的方法,但效果不佳。
<xsl:variable id="newId" select="./newId"/>
<xsl:template match="newId">
<xsl:choose>
<xsl:when test="string-length() < 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'
- string-length 函数需要一个参数
我会这样做:
<?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) < 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>
我没有发现您的模板有什么特别的问题,除了:
<xsl:value-of select="newId"/>
需要:
<xsl:value-of select="."/>
因为您已经处于 newId
的上下文中。
如果您不使用或根本不使用变量,则不明白为什么需要该变量。如果出于某种原因确实需要它,请正确定义它;你现在拥有的会产生错误。
如果条目太长,我想在 XML/XSL 中使用 if 子句对变量进行子字符串化。
我试过类似的方法,但效果不佳。
<xsl:variable id="newId" select="./newId"/>
<xsl:template match="newId">
<xsl:choose>
<xsl:when test="string-length() < 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'
- string-length 函数需要一个参数
我会这样做:
<?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) < 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>
我没有发现您的模板有什么特别的问题,除了:
<xsl:value-of select="newId"/>
需要:
<xsl:value-of select="."/>
因为您已经处于 newId
的上下文中。
如果您不使用或根本不使用变量,则不明白为什么需要该变量。如果出于某种原因确实需要它,请正确定义它;你现在拥有的会产生错误。