2014-09-23 59 views
0

你能帮我转换下面的代码片段吗?包装和移动子节点

<root> 
    <topic>     <!-- First topic --> 
     <p>content1</p> 
     <topic>    <!-- Second topic --> 
      <p>content2</p> 
     </topic> 
     <topic>    <!-- Third topic --> 
      <p>content3</p> 
     </topic> 
    </topic> 
</root> 

我需要削减的第二个主题和第三主题时第一个主题,在/主题包装他们,并与其子根节点附加新的话题。

<root> 
    <topic>     <!-- First topic --> 
     <p>content1</p> 
    </topic> 
    <topic>     <!-- New topic --> 
     <topic>    <!-- Second topic --> 
      <p>content2</p> 
     </topic> 
     <topic>    <!-- Third topic --> 
      <p>content3</p> 
     </topic> 
    </topic> 
</root> 

也许有一个非常简单的解决方案。我试图在第二个/后缀前加上第三个主题(看起来像一个非常肮脏的解决方案),但我无法移动它们。

<xsl:if test="position() = 1"> 
    <![CDATA[ 
    <topic> 
    ]]> 
    ... 
</xsl:if> 

<xsl:if test="position() = last()"> 
    ... 
    <![CDATA[ 
    </topic> 
    ]]> 
</xsl:if> 

UPDATE 1 这是一个更为复杂和详细的示例:

源 - 变换前

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <!-- 
    The root has only a 
    title and multiple child 
    topics. but no other child 
    elements. 
    --> 
    <title>Root</title> 
    <topic id="topic_1"> 
    <!-- 
     Allowed child elements of 'topic' 
     are listed in the DITA spec.: 
     http://bit.ly/1ruYbdq 
    --> 
    <title>First Topic - First Level</title> 
    </topic> 
    <topic id="topic_2"> 
    <title>Second Topic - First Level</title> 
    <!-- 
     This is the main problem. 
     A topic must not contain 
     child topics AND other child 
     elements after the 
     transformation. 
     If a topic has child topic 
     AND other child elements, the 
     topics have to be extracted. 
    --> 
    <topic id="topic_3"> 
     <title>Third Topic - Second Level</title> 
    </topic> 
    <topic id="topic_4"> 
     <!-- 
     The number of topics is not limited. 
     --> 
     <title>Fourth Topic - Second Level</title> 
     <topic id="topic_5"> 
     <!-- 
      Third level topics have to 
      be moved to the second 
      hierarchy level. No topic 
      may reside on the third 
      level after transformation. 
     --> 
     <title>Fifth Topic - Third Level</title> 
     </topic> 
    </topic> 
    </topic> 
</root> 

结果 - 经过转化

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <title>Root</title> 
    <topic id="topic_1"> 
    <title>First Topic - First Level</title> 
    </topic> 
    <topic id="topic_2"> 
    <title>Second Topic - First Level</title> 
    </topic> 
    <!-- 
    The third and fourth topic have 
    been moved extracted from the 
    second topic. Both (could be any 
    number) have been wrapped with a 
    dummy 'topic' element. 
    --> 

    <topic> 
    <!-- 
     The second level topics have 
     been wrapped with a "dummy" 
     topic element. 
    --> 
    <topic id="topic_3"> 
     <title>Fourth Topic - Second Level</title> 
    </topic> 
    <topic id="topic_4"> 
     <title>Fifth Topic - Second Level</title> 
    </topic> 

    <topic id="topic_5"> 
     <!-- 
     The third level topic 
     has been moved to the 
     second hierarchy level. 
     --> 
     <title>Sixth Topic - Third Level</title> 
    </topic> 
    </topic> 
</root> 

回答

0

好吧,这对我的作品:

<xsl:template match="//topic"> 
    <xsl:choose> 
     <xsl:when test="topic[not(topic)]"> 
      <!-- 
       Wrap first level topics in a 
       single <section> element. 
      --> 
      <section> 
       <xsl:apply-templates/> 
      </section> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:choose> 
       <!-- 
        Wrap nested topics in a 
        <section> container. 
       --> 
       <xsl:when test="(count(preceding-sibling::topic) = 0) and (count(following-sibling::topic) >= 1)"> 
        <!-- 
         Close the section of the parent <topic> element. 
        --> 
        <xsl:text disable-output-escaping="yes">&lt;/section&gt;</xsl:text> 
        <xsl:text disable-output-escaping="yes">&lt;section&gt;</xsl:text> 
        <section> 
         <xsl:apply-templates/> 
        </section>  
       </xsl:when> 
       <xsl:otherwise> 
        <section> 
         <xsl:apply-templates/> 
        </section>       
       </xsl:otherwise> 
      </xsl:choose> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
0

也许有一个非常简单的解决方案。

也许有,但很难看到这里给出的是什么,只是一个例子。这个非常简单的解决方案适合你吗?

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="/root"> 
    <xsl:copy> 
     <topic> 
      <xsl:copy-of select="topic/p"/> 
     </topic> 
     <topic> 
      <xsl:copy-of select="topic/topic"/> 
     </topic> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
+0

喜迈克尔,可惜事实并非如此。我准备了一个更复杂的例子及其结果。 – 2014-09-24 22:49:38