2017-10-18 101 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>