ele*_*low 13 html xml xslt variables
我面临的问题似乎很简单,但在XSL的所有内容中都是新手- 我还没有找到合适的解决方案.我想要做的是通过连接foreach元素循环的结果来构建一个字符串,以后我可以将其用作HTML元素属性的值.
鉴于:
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<country>UK</country>
<company>CBS Records</company>
</cd>
<cd>
<country>USA</country>
<company>RCA</company>
</cd>
<cd>
<country>UK</country>
<company>Virgin records</company>
</cd>
</catalog>
Run Code Online (Sandbox Code Playgroud)
期望的输出: CBS;RCA;Virgin records
我需要一个有效的XSLT代码部分,它将以上述方式执行此转换.我相信我需要一个xsl-variable来保存连接结果<company>和分隔符;.如何才能做到这一点?谢谢.
Eri*_*a E 19
我不相信你可以使用XSL变量来连接,因为一旦设置了变量值,它就无法更改.相反,我认为你想要的东西:
<xsl:for-each select="catalog/cd">
<xsl:choose>
<xsl:when test="position() = 1">
<xsl:value-of select="country"/>
</xsl:when>
<xsl:otherwise>
;<xsl:value-of select="country"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
Run Code Online (Sandbox Code Playgroud)
这对你有意义吗?
编辑:刚刚意识到我可能误读了你打算如何使用变量.我上面发布的代码片段可以包装在一个可变元素中供以后使用,如果这是你的意思:
<xsl:variable name="VariableName">
<xsl:for-each select="catalog/cd">
<xsl:choose>
<xsl:when test="position() = 1">
<xsl:value-of select="country"/>
</xsl:when>
<xsl:otherwise>
;<xsl:value-of select="country"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:variable>
Run Code Online (Sandbox Code Playgroud)
如果您可以使用 XSLT 2.0,那么以下任一方法都可以使用:
使用string-join()函数:
<xsl:variable name="companies" select="string-join(catalog/cd/company, ';')" />
Run Code Online (Sandbox Code Playgroud)
使用@separator有xsl:value-of:
<xsl:variable name="companies" >
<xsl:value-of select="catalog/cd/company" separator=";" />
</xsl:variable>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19375 次 |
| 最近记录: |