【问题标题】:How to parse a "/" separated string in XSLT / XPath?如何在 XSLT/XPath 中解析“/”分隔的字符串?
【发布时间】:2018-09-18 12:27:10
【问题描述】:

问题如下:我的 XML 包含内容为“x/y”的元素。这表示进入的“部分”的运行编号。例如在第一个 XML 中,该元素的值为 1/5,第二个为 2/5,最后一个为 5/5。你明白了。元素本身看起来像

<part>x/y</part>

x 可能介于 1 和 y 之间,y 可以是任意数字

我需要找到两种情况的答案:

  1. 当 x=1 时,结果应该是“添加”
  2. 当 x=y 时,结果应为“完成”

如何使用 XSL(1.0 版)解决这个问题?

【问题讨论】:

    标签: xml xslt xslt-1.0


    【解决方案1】:

    使用substring-before():

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="text"/>
    
      <xsl:template match="part">
        <xsl:variable name="x" select="substring-before(., '/')"/>
        <xsl:variable name="y" select="substring-after(., '/')"/>
        <xsl:choose>
          <xsl:when test="$x = 1">Add</xsl:when>
          <xsl:when test="$x = $y">Complete</xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="concat('Unexpected values for x,y: ', $x, ',', $y)"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

    • 谢谢。我以类似的方式添加了变量 y(使用 subsrting-after),并将 $x 与 $y 进行比较以获得 Complete。成功了。
    • @VpSoini:哦,抱歉,在重新阅读您的问题后,我发现我错过了 $x=$y 案例。现在更新(虽然听起来你已经涵盖了它)。谢谢。
    猜你喜欢
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    相关资源
    最近更新 更多