我的XML:
<?xml version="1.0"?>
<Result>
<Answer questionId="Servicios">Auditoría|Asesoría en Impuestos|</Answer>
<Answer questionId="Servicios">Auditoría|Outsourcing|Asesoría en RRHH|</Answer>
</Result>
我想使用xsl:for-each
或类似的东西将每个node()
连接到一个唯一变量(例如<xsl:variable name = "var"/>
),然后使用以下方法计算"|"字符:
<xsl:variable name="total" select="string-length(string($var))-string-length(translate(string($var),'|',''))"/>
如果我这样做:
<xsl:value-of select ="//Result/Answer[@questionId = 'Servicios']//text()"/>
<!--The return is something like an array-->
<!--[1]Auditoría|Asesoría en Impuestos|-->
<!--[2]Auditoría|Outsourcing|Asesoría en RRHH|-->
<!--and the result is '2' it only select the [1] and i need all of them, [1] and [2] in this case-->
我想我必须用xsl:for-each
连接所有的值
我使用xslt version="1.0"
我将感激任何帮助!谢谢!
产生所需结果的最短/最简单的XSLT转换 (Answer
元素的字符串值的连接)是以下:
<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="/">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
当应用于提供的XML文档时:
<Result>
<Answer questionId="Servicios">Auditoría|Asesoría en Impuestos|</Answer>
<Answer questionId="Servicios">Auditoría|Outsourcing|Asesoría en RRHH|</Answer>
</Result>
精确地生成所需的正确结果:
Auditoría|Asesoría en Impuestos|Auditoría|Outsourcing|Asesoría en RRHH|
:
根节点
/
的字符串值是其所有文本节点后代的连接。<xsl:strip-space elements="*"/>
指令从XML文档中消除所有不需要的纯空白文本节点。
Update:如果XML文档比提供的文档更复杂,并且需要一些过滤,这里是一个通用的简单解决方案,使用相同的思想:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vStrings">
<xsl:copy-of select="/*/*[@questionId='Servicios']"/>
</xsl:variable>
<xsl:template match="/">
<xsl:value-of select="$vStrings"/>
</xsl:template>
</xsl:stylesheet>
当应用于这个XML文档时(注意我们必须排除第二个<Answer>
):
<Result>
<Answer questionId="Servicios">Auditoría|Asesoría en Impuestos|</Answer>
<Answer questionId="X">Auditoría|Outsourcing|Asesoría en RRHH|</Answer>
<Answer questionId="Servicios">Auditoría|Outsourcing|Asesoría en RRHH|</Answer>
</Result>
再次生成所需的正确结果:
Auditoría|Asesoría en Impuestos|Auditoría|Outsourcing|Asesoría en RRHH|
对于给定的文档,您可以非常简单地使用normalize-space(Result)
进行连接,但请注意,在计数代码中甚至不需要这样做。
这个样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:value-of select="string-length(Result)-
string-length(translate(Result,'|',''))"/>
</xsl:template>
</xsl:stylesheet>
只输出结果'5',甚至不使用for-each
。
UPDATE
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:variable name="var">
<xsl:apply-templates select="//Answer[@questionId='Servicios']"/>
</xsl:variable>
<xsl:variable name="total" select="string-length($var)-
string-length(translate($var,'|',''))"/>
<xsl:value-of select="$total"/>
</xsl:template>
</xsl:stylesheet>
<!-- This will concatenate the nodes with a comma in between. Is this what you want?-->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/*">
<xsl:for-each select="Answer/text()">
<xsl:value-of select="."/>
<xsl:text>,</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>