2017-10-17 148 views
0

输入xml,如下所示。删除xslt中只指定父元素而非子元素

<part type="frontmatter"> 
<section level="1"><title>THIEME Atlas of Anatomy</title> 
........ 
</section> 
<section level="1"> 
<title></title><para><emph type="bold">To access additional material 
...... 
</section> 
</part> 
<part type="content"> 
<section level="1"> 
<title id="p001">Structure and Development of Organ Systems</title> 
... 
<section level="2"> 
<title>Suggested Readings</title> 
....... 
</section> 
</section> 
</part> 

输出应该是

<part type="frontmatter"> 
<section level="1"><title>THIEME Atlas of Anatomy</title> 
........ 
</section> 
<section level="1"> 
<title></title><para><emph type="bold">To access additional material 
...... 
</section> 
</part> 
<part type="content"> 
<title id="p001">Structure and Development of Organ Systems</title> 
... 
<section level="2"> 
<title>Suggested Readings</title> 
........ 
</section> 
</part> 

我的XSLT是:

<xsl:template match="section[position()=1]"><!-\-//-\-> 
    <xsl:if test="preceding-sibling::part[@type='content'][not(title)]"> 
     <part type="content"> 
     <xsl:apply-templates select="node() | @*"/> 
     </part> 
    </xsl:if> 
</xsl:template> 

我想删除<section level="1">元件,其<part type="content">下出现,其中 “冠军” 元素不应该在这两者之间出现元素。如果“标题”出现在部分元素下,则不应做任何更改。

回答

0

如果你想删除section元素,part元素,在没有前title下,然后在模板匹配应该是这样的

<xsl:template match="part[@type='content']/section[@level='1'][not(preceding-sibling::title)]"> 

试试这个XSLT

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

    <xsl:template match="part[@type='content']/section[@level='1'][not(preceding-sibling::title)]"> 
     <xsl:apply-templates /> 
    </xsl:template> 

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

注,在这个特定的例子中,你可能可以简化到这个

<xsl:template match="part[@type='content']/section[1]"> 

因此,只要删除section元素,如果它是part的第一个子元素。如果您的title之前的元素可能位于section之前,那么这将不起作用。

+0

thanku for you response Tim C.下面的代码工作正常。 Sumathi