2014-09-23 74 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