是否可以使用XSLT检测是否存在(xml)标记?

Inf*_*nd' 3 xml xslt

我想测试标签的存在并根据结果创建一个新节点.

这是输入XML:

<root> 
  <tag1>NS</tag1> 
  <tag2 id="8">NS</tag2> 
  <test> 
    <other_tag>text</other_tag> 
    <main>Y</main> 
  </test> 
  <test> 
    <other_tag>text</other_tag> 
  </test> 
</root> 
Run Code Online (Sandbox Code Playgroud)

并且所需的输出XML是:

<root> 
  <tag1>NS</tag1> 
  <tag2 id="8">NS</tag2> 
  <test> 
    <other_tag>text</other_tag> 
    <Main_Tag>Present</Main_Tag> 
  </test> 
  <test> 
    <other_tag>text</other_tag> 
    <Main_Tag>Absent</Main_Tag>
  </test> 
</root> 
Run Code Online (Sandbox Code Playgroud)

我知道测试标签的价值,但这对我来说是新的.

我尝试使用此模板:(根据要求不能正常工作)

   <xsl:template match="test"> 
      <xsl:element name="test"> 
        <xsl:for-each select="/root/test/*"> 
      <xsl:choose> 
        <xsl:when test="name()='bbb'"> 
          <xsl:element name="Main_Tag"> 
            <xsl:text>Present</xsl:text> 
          </xsl:element> 
        </xsl:when> 
        <xsl:otherwise> 
          <xsl:element name="Main_Tag"> 
          <xsl:text>Absent</xsl:text> 
          </xsl:element> 
        </xsl:otherwise> 
      </xsl:choose> 
        </xsl:for-each> 
      </xsl:element> 
  </xsl:template> 
Run Code Online (Sandbox Code Playgroud)

Rub*_*ias 5

这个怎么样:

<xsl:choose>
    <xsl:when test="main = 'Y'">
        <Main_Tag>Present</Main_Tag> 
    </xsl:when>
    <xsl:otherwise>
        <Main_Tag>Absent</Main_Tag>
    </xsl:otherwise>
</xsl:choose>
Run Code Online (Sandbox Code Playgroud)

要么

<Main_Tag>
    <xsl:choose>
        <xsl:when test="main = 'Y'">Present</xsl:when>
        <xsl:otherwise>Absent</xsl:otherwise>
    </xsl:choose>
</Main_Tag>
Run Code Online (Sandbox Code Playgroud)