外观相似的XSLT转换不起作用

外观相似的XSLT转换不起作用,xslt,xslt-1.0,xslt-2.0,Xslt,Xslt 1.0,Xslt 2.0,输入XML <Web-inf> <A> <A1>Val1</A1> <A1>Val1</A1> <A1>Val1</A1> </A> <A> <A1>Val2</A1> <A1>Val2</A1> <A1>Val2</A1> </A&g

输入XML

<Web-inf>
  <A>
    <A1>Val1</A1>
    <A1>Val1</A1>
    <A1>Val1</A1>
  </A>

  <A>
    <A1>Val2</A1>
    <A1>Val2</A1>
    <A1>Val2</A1>
  </A>

  <B>
   <B1>Hi</B1>
  </B>

  <B>
   <B1>Bye</B1>   
  </B>

  <C>DummyC</C>

  <D>DummyD</D>

</Web-inf>

瓦尔1
瓦尔1
瓦尔1
瓦尔2
瓦尔2
瓦尔2
你好
拜伊
DummyC
达姆伊德
我想添加
标记,如果它还不存在,
值为“早”和“晚”。如果它存在,我什么也不做。我写了下面的转换,但奇怪的是,只有后一个有效,而第一个完全被忽略。因此,只有
标记一起插入。这是一个已知的问题吗?如果是,如何更正

<xsl:output method="xml" indent="yes" />

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Web-inf[not(B[B1='Morning'])]/B[last()]">
    <xsl:copy-of select="*" />
      <B>
         <B1>Morning</B1>
      </B>
</xsl:template>

<xsl:template match="Web-inf[not(B[B1='Evening'])]/B[last()]">
    <xsl:copy-of select="*" />
      <B>
         <B1>Evening</B1>
      </B>
</xsl:template>

早晨
傍晚
我希望O/p XML如下所示

Output.xml

<Web-inf>
  <A>
    <A1>Val1</A1>
    <A1>Val1</A1>
    <A1>Val1</A1>
  </A>

  <A>
    <A1>Val2</A1>
    <A1>Val2</A1>
    <A1>Val2</A1>
  </A>

  <B>
   <B1>Hi</B1>
  </B>

  <B>
   <B1>Bye</B1>   
  </B>

  <B>
   <B1>Morning</B1>   
  </B>
  <B>
   <B1>Evening</B1>   
  </B>

  <C>DummyC</C>

  <D>DummyD</D>

</Web-inf>

瓦尔1
瓦尔1
瓦尔1
瓦尔2
瓦尔2
瓦尔2
你好
拜伊
早晨
傍晚
DummyC
达姆伊德

对于给定的输入XML,
B[last()]
的两个模板都将匹配。当两个模板匹配具有相同优先级的元素时,这将被视为错误。XSLT处理器将标记错误,或者忽略除最后一个匹配模板之外的所有模板

在这种情况下,最好使用单个模板匹配
B[last()]
,并在模板中使用其他条件作为
xsl:if
语句

试试这个XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Web-inf/B[last()]">
        <xsl:copy-of select="*" />
        <xsl:if test="not(../B[B1='Morning'])">
          <B>
             <B1>Morning</B1>
          </B>
        </xsl:if>
        <xsl:if test="not(../B[B1='Evening'])">
          <B>
             <B1>Evening</B1>
          </B>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

早晨
傍晚