2011-04-25 65 views
3

我的XML:的毗连几个节点XSL

<?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> 

我想每个node() Concat的到使用xsl:for-each或类似的东西了独特的可变(<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--> 

我想我必须使用XSLT串接所有值与xsl:for-each

IM version="1.0"

我希望得到任何帮助! 谢谢!

+0

洛佩兹:什么是这个级联的结果呢? – 2011-04-25 22:05:11

+0

@Ajjandro类似于“Auditoría|Asesoríaen Impuestos |Auditoría|外包|Asesoríaen RRHH |” – christiangobo 2011-04-25 22:12:10

+0

好问题,+1。查看我的答案,了解产生所需结果的最短和最简单的XSLT转换。还提供了广泛的解释。 :) – 2011-04-26 03:23:05

回答

2

对于给定的文件,您可以连接与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后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> 
+0

感谢mousio我编辑了代码,告诉你我需要什么 – christiangobo 2011-04-25 23:20:45

+0

现在只需要使用默认模板(仍然没有'for-each'),也称为* built-in template rule *,如[5.8节W3C XSLT 1.0推荐](http://www.w3.org/TR/xslt#built-in-rule)。 – mousio 2011-04-25 23:55:44

+0

使用'copy-of'而不是'apply-templates'可以在这里得到相同的结果。无需为这个特定问题去除空白;如果你以后需要它,最好在'normalize-space()'上面使用'strip-space'(参见Dimitre的回答)。 – mousio 2011-04-26 14:31:50

0
<!-- 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> 
3

产生想要的结果(该Answer元素的字符串值的串接)最短/最简单的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="/"> 
    <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| 

说明

  1. 根节点/的字符串值是的串接它的所有文本节点后代。

  2. <xsl:strip-space elements="*"/>指令从XML文档中消除了所有不需要的只有空白的文本节点。

更新:如果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| 
+0

是的,带有'xsl:strip-space'声明的'string(/ Result)'会做到这一点。除了计数要求之外,从不合格的问题看,需要进行一些过滤(在'@ questionId'上)。也许一个RTF变量'xsl:copy-of select =“/ Result/Answer [@ questionId ='Servicios']”'... – 2011-04-26 03:37:51

+0

@Alejandro:是的,谢谢你让我意识到这个要求。我已将我的解决方案更新为包含过滤。 – 2011-04-26 03:49:51

+0

@Alejandro @mousio感谢您的帮助! – christiangobo 2011-04-26 13:35:26