检查多个事件并使用XSLT对其进行操作

时间:2016-03-22 11:59:21

标签: xslt xslt-1.0

我有一个要求,我需要在一个条件中检查两个匹配项的所有四个值,并且不会给出hey1hello1一次出现hey2另一个是hello2。 我的意思是无法保证hey1hey2作为第一个或第二个进入;这同样适用于hello1hello2

我使用下面的代码为我提供输出<output>ININ</output>,但我需要<output>IN</output>

我正在尝试此POC,我在下面有一个示例XML:

<q>
        <a>
          <b>hey1</b>
          <c>hello1</c>
        </a>
        <a>
          <b>hey2</b>
          <c>hello2</c>
        </a>
</q>

请提供一个解决方案,检查所有四个条件,并为每个if/when使用一个<a>条件将发生两次或多次。

     <?xml version="1.0" encoding="UTF-8"?>
        <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <xsl:output encoding="UTF-8" version="1.0" method="xml" indent="yes"/>
        <xsl:template match="/">
        <xsl:variable name="a" select="/q/a"/>
          <output>
                <xsl:for-each select="$a">
                    <xsl:if test="$a/b='hey1' and $a/c= 'hello1' and $a/b='hey2' and $a/c= 'hello2'">
                        <xsl:value-of select="'IN'"/>
                    </xsl:if>
                </xsl:for-each>
            </output>
        </xsl:template>
    </xsl:stylesheet>        

我希望我的问题足够明确,可以回答这个问题。 谢谢你的任何建议。

1 个答案:

答案 0 :(得分:0)

尝试:

<xsl:template match="q">
    <xsl:if test="a[b='hey1' and c='hello1'] and a[b='hey2' and c='hello2'] ">
        <xsl:text>IN</xsl:text>
    </xsl:if>
</xsl:template>

这将为两者返回"IN"

<q>
   <a>
      <b>hey1</b>
      <c>hello1</c>
   </a>
   <a>
      <b>hey2</b>
      <c>hello2</c>
   </a>
</q>

<q>
   <a>
      <b>hey2</b>
      <c>hello2</c>
   </a>
   <a>
      <b>hey1</b>
      <c>hello1</c>
   </a>
</q>

但不适用于:

<q>
   <a>
      <b>hey1</b>
      <c>hello2</c>
   </a>
   <a>
      <b>hey2</b>
      <c>hello1</c>
   </a>
</q>