Html 在 foreach 循环中构建 XSLT 字符串(变量)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11919117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
building an XSLT string (variable) in a foreach loop
提问by electroglow
The problem I'm facing seems simple, but being a novice in everything XSL- I'm yet to find a proper solution. What I want to do is build a string by concatenating the results of a foreachelement loop, that I can later on use as a value for an attribute of a HTML element.
我面临的问题似乎很简单,但作为XSL的新手,我还没有找到合适的解决方案。我想要做的是通过连接foreach元素循环的结果来构建一个字符串,稍后我可以将其用作 HTML 元素属性的值。
Given:
鉴于:
<?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>
Desired output: CBS;RCA;Virgin records
期望的输出: CBS;RCA;Virgin records
I need a valid portion of XSLTcode that would perform this transformation in a way described above. I believe I need an xsl-variablethat would hold the result of concatenating <company>
and a separator character ;
. How can this be done? Thank you.
我需要一个有效的XSLT代码部分,以上述方式执行此转换。我相信我需要一个xsl 变量来保存连接<company>
和分隔符的结果;
。如何才能做到这一点?谢谢你。
回答by Erika E
I don't believe you can use XSL variables to concatenate, because once a variable value is set, it can't be changed. Instead, I think you want something like:
我不相信您可以使用 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>
Does that make sense to you?
这对你有意义吗?
Edit: Just realized I may have misread how you were intending to use the variable. The snippet I posted above can be wrapped in a variable element for later use, if that's what you meant:
编辑:刚刚意识到我可能误读了您打算如何使用该变量。我上面发布的代码段可以包装在一个变量元素中供以后使用,如果这就是你的意思:
<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>
回答by Dimitre Novatchev
Here is one simple, true XSLT solution:
这是一个简单的、真正的 XSLT 解决方案:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="company">
<xsl:value-of select="."/>
<xsl:if test="following::company">;</xsl:if>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
当此转换应用于提供的 XML 文档时:
<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>
the wanted, correct result (all companies concatenated together and delimited by a ;
) is produced:
产生了想要的、正确的结果(所有公司连接在一起并由 a 分隔;
):
CBS Records;RCA;Virgin records
回答by Mads Hansen
If you can use XSLT 2.0 then either of the following would work:
如果您可以使用 XSLT 2.0,那么以下任一方法都可以使用:
Use the string-join()
function:
使用string-join()
函数:
<xsl:variable name="companies" select="string-join(catalog/cd/company, ';')" />
Use @separator
with xsl:value-of
:
使用@separator
有xsl:value-of
:
<xsl:variable name="companies" >
<xsl:value-of select="catalog/cd/company" separator=";" />
</xsl:variable>