5

大家好,我正在尝试为 XML 中的标记设置一个固定值,以与条件值进行比较。如

<xsl:when test="(//TestInput='XYZA') OR (//TestInput='XYZB') OR (//TestInput='XYZC') OR (//TestInput='XYZD')">abcd</xsl:when>

当我尝试使用带有标签的 XML 运行转换时<TestInput>,它给了我一个错误

Extra illegal tokens: '(', '/', '/', 'TestInput', '=', ''XYZA'', ')', 'OR', '(', '/', '/', 'TestInput', '=', ''XYZB'', ')', 'OR', '(', '/', '/', 'TestInput', '=', ''XYZC'', ')', 'OR', '(', '/', '/', 'TestInput', '=', ''XYZD'', ')'

请帮助我根据条件在 where 子句中使用 OR 运算符设置此标签的值。

提前致谢

4

3 回答 3

13

or大小写对 XML/XSLT/XPath 很重要,因此请使用OR.

于 2012-07-23T17:56:00.973 回答
4

我使用以下 XML 创建了一个测试:

<?xml version="1.0" encoding="utf-8"?>
<Test>
  <Item>DAC</Item>
  <Item>DAD</Item>
  <Item>DAE</Item>
</Test>

以及以下 XSLT:

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

    <xsl:template match="@* | node()">
      <test>
      <xsl:choose>
        <xsl:when test="//Item[text()='DAC'] or //Item[text()='DAE']">
          <output>Here is the text!</output>
        </xsl:when>
        <xsl:otherwise></xsl:otherwise>
      </xsl:choose>
      </test>
    </xsl:template>
</xsl:stylesheet>

我得到以下输出:

<?xml version="1.0" encoding="utf-8"?>
<test>
  <output>Here is the text!</output>
</test>

使用 Micorosft.Net 的 XmlDocument 类进行测试。关键的区别在于' or ' 语句的情况。

于 2012-07-23T17:57:11.490 回答
0

问题是 //TestInput 选择了一组元素,而不仅仅是一个。

你应该使用 xsl:for-each

<xsl:for-each select="//TestInput[.='a'or'b'or'c']">abc</xsl:for-each>
于 2012-07-23T17:59:48.057 回答