XPath 1.0,将十六进制属性解释为数字

XPath 1.0, Interpret hex attribute as a number

我需要比较 XML 表示整数的属性,但可以使用 XPath/XSLT-1.0.

以十进制或十六进制(前缀为 0x)给出

这里有一个(不工作的)XSLT 来演示:

<?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" encoding="iso-8859-1" omit-xml-declaration="yes" />

    <xsl:template match="//node">
        <xsl:if test="@value &gt; 2">
            <xsl:value-of select="@value"/>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

这是一个XML输入:

<?xml version="1.0" encoding="UTF-8"?>
<body>
    <node value="1"/>
    <node value="3"/>
    <node value="0x03"/>
</body>

这是所需的输出。格式并不重要;重要的只是第二个和第三个节点匹配:

3 0x03

第二个节点上只有一个匹配; XML 未将十六进制节点解释为数字。谁能想出一个合理的方案来解决这个问题?

The format is not important;

为了方便起见,我将以 XML 格式作为输出进行演示:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/body">
    <xsl:copy>
        <xsl:for-each select="node">
            <xsl:variable name="decimal">
                <xsl:choose>
                    <xsl:when test="starts-with(@value, '0x')">
                        <xsl:call-template name="hex2num">
                            <xsl:with-param name="hex" select="substring-after(@value, '0x')"/>
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="@value"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
            <xsl:if test="$decimal > 2">
                <xsl:copy-of select="."/>
            </xsl:if>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

<xsl:template name="hex2num">
    <xsl:param name="hex"/>
    <xsl:param name="num" select="0"/>
    <xsl:param name="MSB" select="translate(substring($hex, 1, 1), 'abcdef', 'ABCDEF')"/>
    <xsl:param name="value" select="string-length(substring-before('0123456789ABCDEF', $MSB))"/>
    <xsl:param name="result" select="16 * $num + $value"/>
    <xsl:choose>
        <xsl:when test="string-length($hex) > 1">
            <xsl:call-template name="hex2num">
                <xsl:with-param name="hex" select="substring($hex, 2)"/>
                <xsl:with-param name="num" select="$result"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$result"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

应用于以下测试输入

<body>
    <node value="1"/>
    <node value="0x02"/>
    <node value="3"/>
    <node value="0x04"/>
    <node value="0xB1"/>
</body>

产生这个结果:

<?xml version="1.0" encoding="UTF-8"?>
<body>
   <node value="3"/>
   <node value="0x04"/>
   <node value="0xB1"/>
</body>

既然你说你的处理器是 MSXSL,你可以利用 msxsl 扩展,它允许你 define a script 可以用于 XSLT 处理器本身无法完成的工作。

下面使用一个小的 JScript 函数,将所有以 0x 开头的十六进制数转换为对应的十进制数。

<xsl:stylesheet
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:script="http://tempuri.org/script"
  exclude-result-prefixes="msxsl script"
>
  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

  <xsl:template match="/body">
    <xsl:copy>
      <xsl:copy-of select="node[script:hexToDec(@value) &gt; 2]" />
    </xsl:copy>
  </xsl:template>

  <msxsl:script language="jscript" implements-prefix="script"><![CDATA[
    function hexToDec(nodeList) {
      var firstNode, matches;
      if (nodeList.length) {
        firstNode = nodeList.nextNode();
        matches = /^\s*0x0*([0-9A-F]+)\s*$/i.exec(firstNode.text);
        return matches ? parseInt(matches[1], 16) : firstNode.text;
      }
      return "";
    }
  ]]></msxsl:script>
</xsl:stylesheet>

msxsl 命名空间还允许使用更高级的方法来扩展 XSLT 处理器,例如使用 COM DLL 或 .NET 代码,但对于这种简单的情况,JScript 就可以了。