2017-10-17 13 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> 

私は「タイトル」要素は、これら2つの間に表示されるべきではないどこ<part type="content">の下に表示されている<section level="1">要素を削除したいです要素。 part要素の下に "title"が表示された場合は、変更しないでください。

答えて

0

あなたは何titleの前には存在しないpart要素、下、section要素を削除したい場合は、テンプレートの一致がこの

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

ようになっているはずですが

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

(注)このXSLTをお試しくださいこの特定の例では、これに簡略化することができます

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

section要素がpartの最初の子要素である場合は、それを削除してください。 sectionの前にtitleという要素以外の要素がある場合は、これは機能しません。

+0

ありがとうございます。Tim C.以下のコードは正しく機能しています。 /section [@ level = '1'] [xsl:template] Sumathi

関連する問題