如何在XSLT / XPath中解析“/”分隔的字符串?

问题描述 投票:1回答:1

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

<part>x/y</part>

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

我需要找到两个案例的答案:

  1. 当x = 1时,结果应为“Add”
  2. 当x = y时,结果应为“完全”

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

xml xslt xslt-1.0
1个回答
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>
© www.soinside.com 2019 - 2024. All rights reserved.