我的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"
如果有任何帮助,我将不胜感激!谢谢!
发布于 2011-04-26 06:21:13
对于给定的文档,您可以非常简单地使用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。
OP编辑后的更新:
<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> 发布于 2011-04-26 11:16:28
生成所需结果的最短/最简单的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|说明
XMLXML根节点的字符串值/是其所有文本节点的串联
/文档中消除所有不需要的纯空格文本节点。:如果文档比所提供的文档更复杂,并且需要一些过滤,这里有一个通用而简单的解决方案,使用相同的思想:
<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|发布于 2011-04-26 05:53:42
<!-- 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>https://stackoverflow.com/questions/5783591
复制相似问题