XSLT - 在text()节点中的给定位置之前获取子字符串

时间:2018-02-02 10:38:11

标签: xml xslt xslt-2.0

我在文档中有一个像这样的xml节点,

<full>Solid biofuels - Determination of moisture content - Oven dry method - Part 2: Total moisture - Simplified method</full>

注意:<full>中的text()节点可以是任何内容。 -

中可以显示任意数量的<full>

预期输出是,

<full>
    <p1>Solid biofuels - Determination of moisture content - Oven dry method -</p1>
    <p2>Part 2: Total moisture - Simplified method</p2>
</full>

我需要在-的第3 <p1>之前获取内容,其余内容应该在<p2>内。

我正在使用XSLT来实现这个目标。我尝试过使用XSLT正则表达式,tokanize()函数,substring()函数但无法找到合适的方法。

<xsl:template match="full">
    <full>
        <xsl:for-each select="tokenize(.,'-')">
            <p1>
                <xsl:if test="position()=1 or position()=2">
                    <xsl:value-of select="."/>
                    <xsl:text>-</xsl:text>
                </xsl:if>
            </p1>
            <p2>
                <xsl:if test="position() gt 2">
                    <xsl:value-of select="."/>
                    <xsl:text>-</xsl:text>
                </xsl:if>
            </p2>
        </xsl:for-each>
    </full>
</xsl:template>

任何人都可以建议我这样做的方法。

1 个答案:

答案 0 :(得分:2)

以下是使用tokenize

执行此操作的方法
<xsl:template match="full">
  <xsl:copy>
      <xsl:variable name="tokens" select="tokenize(text(), ' - ')" />
      <xsl:variable name="tokenCount" select="count($tokens)" />
      <p1>
          <xsl:value-of select="$tokens[position() le 3]" separator=" - " />
          <xsl:if test="$tokenCount > 3"> - </xsl:if>
      </p1>
      <xsl:if test="$tokenCount > 3">
        <p2>
          <xsl:value-of select="$tokens[position() gt 3]" separator=" - " />
        </p2>
      </xsl:if>
  </xsl:copy>
</xsl:template>
相关问题