2017-08-17 45 views
2

我有一個文檔,其中包含各種位置的註釋節點。我想將這些註釋移動到文檔中的一個新位置,並將它們轉換爲p元素。如何將XML註釋節點轉換並移動到不同位置的元素節點

我是一位XSLT初學者,在W3schools和StackFlow的幫助下,我已經能夠獲得這些評論並轉換到正確的位置。但是,轉換後的註釋會被複制,而不會移動,因此它們會保留在其原始位置。

例如,給出下面的輸入:

<?xml version="1.0" encoding="UTF-8"?> 
<!--Comment before root element--> 
<concept> 
    <!--Comment element is parent--> 
    <title>Test Topic</title> 
    <shortdesc>This is a shortdesc element <!-- shortdesc element is parent --></shortdesc> 
    <conbody> 
     <!-- Conbody element is parent --> 
     <p>This is para 1 </p> 
     <section> 
     <!--Section element is parent; comment is before title--> 
      <title>Section 1</title> 
      <!--Section element is parent; comment is after title--> 
      <p>This is para 1 in section1</p> 
     </section> 
    </conbody> 
</concept> 

我想下面的輸出:

<?xml version="1.0" encoding="UTF-8"?> 
<concept> 
    <title>Test Topic</title> 
    <shortdesc>This is a shortdesc element </shortdesc> 
    <conbody> 
    <p>This is para 1 </p> 
    <section> 
     <title>Section 1</title> 
     <p>This is para 1 in section1</p> 
    </section> 
    <section outputclass="authorNote"> 
     <p>Comment before root element </p> 
     <p>Comment element is parent</p> 
     <p>shortdesc element is parent</p> 
     <p>Conbody element is parent</p> 
     <p>Section element is parent; comment is before title</p> 
     <p>Section element is parent; comment is after title</p> 
    </section> 
</conbody> 

這是我的樣式表:

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="xml" encoding="UTF-8"/> 

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

    <!-- Create new section to hold converted comments --> 
    <xsl:template match="conbody" > 
     <xsl:copy> 
      <xsl:apply-templates/> 
      <section outputclass="authorNote"> 
        <xsl:apply-templates select="//comment()"/> 
      </section> 
     </xsl:copy>  
    </xsl:template> 

    <!-- Convert comment nodes to p elements --> 
    <xsl:template match="//comment()"> 
      <p><xsl:value-of select="."/></p>   
    </xsl:template> 

</xsl:stylesheet> 

即產生s以下輸出:

<?xml version="1.0" encoding="UTF-8"?> 
<p>Comment before root element </p> 
<concept> 
    <p>Comment element is parent</p> 
    <title>Test Topic</title> 
    <shortdesc>This is a shortdesc element <p>shortdesc element is parent</p></shortdesc> 
    <conbody> 
     <p>Conbody element is parent</p> 
     <p>This is para 1 </p> 
     <section> 
      <p>Section element is parent; comment is before title</p> 
      <title>Section 1</title> 
      <p>Section element is parent; comment is after title</p> 
      <p>This is para 1 in section1</p> 
     </section> 
     <section outputclass="authorNote"> 
      <p>Comment before root element </p> 
      <p>Comment element is parent</p> 
      <p>shortdesc element is parent</p> 
      <p>Conbody element is parent</p> 
      <p>Section element is parent; comment is before title</p> 
      <p>Section element is parent; comment is after title</p> 
     </section> 
    </conbody> 
</concept> 

我在做什麼錯了?到目前爲止我發現的文章都是關於操縱元素的,而不是評論節點。有人能給我一些關於如何解決這個問題的提示嗎?

回答

3

分開的兩個任務,模式(不要複製使用默認模式的意見,用不同的方式轉換它們):

<!-- suppress treatment of comments through default mode --> 
<xsl:template match="comment()"/> 

<!-- Create new section to hold converted comments --> 
<xsl:template match="conbody" > 
    <xsl:copy> 
     <xsl:apply-templates/> 
     <section outputclass="authorNote"> 
       <xsl:apply-templates select="//comment()" mode="transform"/> 
     </section> 
    </xsl:copy>  
</xsl:template> 

<!-- Convert comment nodes to p elements --> 
<xsl:template match="comment()" mode="transform"> 
     <p><xsl:value-of select="."/></p>   
</xsl:template> 

http://xsltransform.net/93dEHGn在線樣本。