2017-08-17 17 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在线样本。