如何将XML注释节点转换并移动到不同位置的元素节点

时间:2017-08-17 13:59:24

标签: xslt xslt-2.0

我有一个文档,其中包含各种位置的注释节点。我想将这些注释移动到文档中的单个新位置,并将它们转换为p元素。

我是一名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>

产生以下输出:

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

我做错了什么?到目前为止我发现的文章都是关于操纵元素,而不是注释节点。有人可以给我一些如何解决这个问题的技巧吗?

1 个答案:

答案 0 :(得分: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的在线示例。