Xml 根据xslt中的子元素字符串注释父元素

Xml 根据xslt中的子元素字符串注释父元素,xml,xslt,Xml,Xslt,我的输入XML如下图所示,该部分显示在“部分”下 <part type="backmatter"> <section counter="yes" level="1"> <title>Credits<target id="page1301"/></title> <section counter="yes" level="2"> <title>Chapter 1</title> <para>&

我的输入XML如下图所示,该部分显示在“部分”下

<part type="backmatter">
<section counter="yes" level="1">
<title>Credits<target id="page1301"/></title>
<section counter="yes" level="2">
<title>Chapter 1</title>
<para><link idref="c001_t003">Table 1-3</link> Adapted from Sidle DM</para>
......
......
</section>
</section>
<section counter="yes" level="1">
<title>Index<target id="page1321"/></title>
<section counter="yes" level="2">
<title>A</title>
<listing type="dash">
<litem><para>Abbé lip switch flap</para>
</litem>
<litem><para>Abdomen</para>
......
......
</section>
</section>
</part>

信用
第一章
表1-3改编自Sidle DM
......
......
指数
A.
Abbélip开关活门
腹部
......
......
产出应该是,

<part type="backmatter">
<section counter="yes" level="1">
<title>Credits<target id="page1301"/></title>
<section counter="yes" level="2">
<title>Chapter 1</title>
<para><link idref="c001_t003">Table 1-3</link> Adapted from Sidle DM</para>
......
......
</section>
</section>
<!--<section counter="yes" level="1">
<title>Index<target id="page1321"/></title>
<section counter="yes" level="2">
<title>A</title>
<listing type="dash">
<litem>
<para>Abbé lip switch flap</para></litem>
<litem><para>Abdomen</para>
......
......
</section>
</section>-->
</part>

信用
第一章
表1-3改编自Sidle DM
......
......
我的xslt是

<xsl:template match="section">        
<xsl:choose>
<xsl:when test="following-sibling::title[contains(., 'Index')]">
<xsl:text disable-output-escaping="yes">&lt;!--</xsl:text>
<xsl:copy><xsl:apply-templates select="node() | @*"/>  </xsl:copy>
<xsl:text disable-output-escaping="yes">--&gt;</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:copy><xsl:apply-templates select="node() | @*"/>  </xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template> 

!--
--

每当标题包含'Index'字符串时,我想对“section”进行注释,其余的部分应该与xml中的一样。您能帮我们解决这个问题吗。

它几乎可以正常工作,您只需要稍微改变一下您的状况:

<xsl:choose>
  <xsl:when test="contains(title, 'Index')">
    <xsl:text disable-output-escaping="yes">&lt;!--</xsl:text>
    <xsl:copy><xsl:apply-templates select="node() | @*"/></xsl:copy>
    <xsl:text disable-output-escaping="yes">--&gt;</xsl:text>
  </xsl:when>
  <xsl:otherwise>
    <xsl:copy><xsl:apply-templates select="node() | @*"/></xsl:copy>
  </xsl:otherwise>
</xsl:choose>

!--
--