连接两个节点值

Concatenate two node values

我有下面的XML结构,我需要合并handlingInstructionText的值:

<handlingInstruction>
    <handlingInstructionText>CTAC  |  MARTINE HOEYLAERTS</handlingInstructionText>
</handlingInstruction>
<handlingInstruction>
    <handlingInstructionText>PHON  |  02/7225235</handlingInstructionText>
</handlingInstruction>

我的预期输出是

CTAC  |  MARTINE HOEYLAERTS PHON  |  02/7225235

我目前正在使用字符串连接函数,但我目前使用的 xsl 版本似乎不支持它。

<xsl:value-of select="otxsl:var-put('Join2_handlingInstructionText',
string-join(handlingInstruction/concat(handlingInstructionText/text(),
' ', handlingInstructionText/text())))" />

我已经尝试使用 for-each 函数来获取每个值,但我希望它只包含 1 行代码。

XSLT 1.0

<xsl:value-of select="concat(handlingInstruction[1]/handlingInstructionText,
                             ' ',
                             handlingInstruction[2]/handlingInstructionText)"/>

将 return 您的预期输出:

CTAC | MARTINE HOEYLAERTS PHON | 02/7225235

对于您给定的输入 XML:

<r>
  <handlingInstruction>
      <handlingInstructionText>CTAC  |  MARTINE HOEYLAERTS</handlingInstructionText>
  </handlingInstruction>
  <handlingInstruction>
      <handlingInstructionText>PHON  |  02/7225235</handlingInstructionText>
  </handlingInstruction>
</r>

假设r是当前节点。 Click to try


更新:因此,在您的 var-put 扩展的上下文中,这将是:

<xsl:value-of select=
              "otxsl:var-put('Join2_handlingInstructionText',
                              concat(handlingInstruction[1]/handlingInstructionText,
                                     ' ',
                                     handlingInstruction[2]/handlingInstructionText))"/>

I already tried using a for-each function to get each value but I want it to make it a 1 line code only.

你应该把它写成尽可能多的行。碰巧,您可以使用:

<xsl:apply-templates select="handlingInstruction/handlingInstructionText"/>

产生期望的结果,但是:

<xsl:for-each select="handlingInstruction">
    <xsl:value-of select="handlingInstructionText"/>
</xsl:for-each>

也很好。


注意:以上两个建议都假定 well-formed 输入,例如:

<root>
    <handlingInstruction>
        <handlingInstructionText>CTAC  |  MARTINE HOEYLAERTS</handlingInstructionText>
    </handlingInstruction>
    <handlingInstruction>
        <handlingInstructionText>PHON  |  02/7225235</handlingInstructionText>
    </handlingInstruction>
</root>

和一个匹配 root.

的模板