我有一个类似于下面结构的 xml。
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="eng">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>
我已经提取了所有title
节点为 <xsl:variable name="titles" select="/bookstore/book/title"/>
。现在,我想将这些标题连接起来,将每个标题用单引号引起来,然后用逗号分隔它们并将它们存储在变量中,以便输出如下所示: 'Harry Potter','Learning XML'
。我怎样才能做到这一点?
最佳答案
已知的值列表可以通过concat()
“组合在一起”。但在您的情况下,您不知道有多少项目属于您的列表(以标题形式),xlst-1.0 中唯一的可能性是迭代到元素(for-each
或 apply-模板
并连接它们。
试试这个:
<xsl:variable name="titles" select="/bookstore/book/title"/>
<xsl:variable name="titles_str" >
<xsl:for-each select="$titles" >
<xsl:if test="position() > 1 ">, </xsl:if>
<xsl:text>'</xsl:text>
<xsl:value-of select="."/>
<xsl:text>'</xsl:text>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$titles_str"/>
关于xslt - 连接所有元素的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17289687/