如果找到节点则返回真

ReturnTrue if node is found

如果找到特定节点,我需要形成新元素,但如果该特定节点多次出现,我只需要形成一个元素,我的代码正在形成 aifound 元素两次。

<root>
  <ai>
    <i></i>
  </ai>
  <ai>
    <i></i>
  </ai>
</root>

输出xml


    <root>
    <ai>
    <i></i>
    </ai>
    <ai>
    <i></i>
    </ai>
    <aifound>True</aifound>
    </root>

我的 xslt

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

  <xsl:strip-space elements="*"/>

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

简单的怎么样:

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="/root">
    <xsl:copy>
        <xsl:copy-of select="*"/>
        <aifound>
            <xsl:value-of select="boolean(ai)"/>
        </aifound>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

不清楚如果根本没有 ai 是否要添加 aifound。以下 xslt 仅在 ai 存在时才添加 aifound

<?xml version="1.0" encoding="UTF-8"?>
<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-of select="."/>
    </xsl:template>
    
    <xsl:template match="root">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <xsl:if test="//ai">
                <aifound>True</aifound>    
            </xsl:if>
        </xsl:copy>        
    </xsl:template>
    
</xsl:stylesheet>