2017-10-18 127 views
1

我的输入XML就像下面的部分出现在部分下。根据xslt中的子元素字符串评论父元素

<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> 

输出应该是,

<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> 

我的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> 

我要评论 “部分” 只要标题包含 '索引' 的字符串,其余的部分应该是因为它在xml中。你能帮我们解决这个问题吗?

回答

2

它几乎工作,你只需要改变你的条件的位:

<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>