2011-03-01 76 views
1

我在這裏做了一些搜索,發現了一些與我的問題有關的問題,但是我仍然遇到了麻煩......(希望我沒關係,米增加一個新的問題,而不是評論對現有..)將某些選擇節點的兄弟節點移動到一個新的父節點中

<?xml version="1.0"?> 
<section> 
    <title>Main Section</title> 
    <para>Here is some text that is a child of a main section.</para> 
    <para>Some more text.</para> 
    <para>When a section has subsections, it should not have loose paragraphs before the first sub section. Those loose paras should be placed inside a comment element.</para> 
    <section> 
     <title>This is my subsection</title> 
     <para>Text that is inside of the sub-section</para> 
     <para>And some more sub section text.</para> 
    </section> 
</section> 

我想有/節/第置於一個新創建的註釋節點內,像這樣:

<?xml version="1.0"?> 
<section> 
    <title>Main Section</title> 
    <comment> 
     <para>Here is some text that is a child of a main section.</para> 
     <para>Some more text.</para> 
     <para>When a section has subsections, it should not have loose paragraphs before the first sub section. Those loose paras should be placed inside a comment element.</para> 
    </comment> 
    <section> 
     <title>This is my subsection</title> 
     <para>Text that is inside of the sub-section</para> 
     <para>And some more sub section text.</para> 
    </section> 
</section> 

我嘗試了一些我發現搜索stackoverflow的建議,最接近的是here.

這是我使用的樣式表:

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


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


<!-- paras that are children of a section that has direct para children and dircect section children --> 
<xsl:template match="section[para][section]/para[1]"> 
    <comment> 
     <xsl:apply-templates select="../para" mode="commentPara"/> 

    </comment> 
</xsl:template> 

<xsl:template match="*" mode="commentPara"> 
    <xsl:call-template name="identity"/> 
</xsl:template> 

<xsl:template match="section[para][section]/para"/> 


</xsl:stylesheet> 

它輸出這樣的:

<?xml version='1.0' ?> 
<section> 
    <title>Main Section</title> 



    <section> 
     <title>This is my subsection</title> 
     <para>Text that is inside of the sub-section</para> 
     <para>And some more sub section text.</para> 
    </section> 
</section> 

簡單地刪除我想在一個註釋標記來包裝段。我試着基本上一行一行地通過問題中的樣式錶鏈接到...任何想法? 謝謝, bp

+0

親愛的壞人,這是一個很好的問題+1。查看我的答案,獲得一個完整的,簡單的,簡短的和簡單的解決方案,它可能比其他解決方案更有效,而不是使用密鑰。 :) –

回答

3

這就是分組鄰接para元素。

問題出在Conflict Resolution for Template Rules:因爲兩個para匹配規則具有相同的導入優先級和相同的計算優先級,錯誤恢復機制可能導致應用最後一個。

你需要像「第一para兒」規則明確定義@priority

<xsl:template match="section[para][section]/para[1]" priority="1"> 
</ 

有了這樣的修改,輸出爲:

<?xml version="1.0" encoding="UTF-16"?> 
<section> 
    <title>Main Section</title> 
    <comment> 
     <para>Here is some text that is a child of a main section.</para> 
     <para>Some more text.</para> 
     <para>When a section has subsections, it should not have loose paragraphs before the first sub section. Those loose paras should be placed inside a comment element.</para> 
    </comment> 
    <section> 
     <title>This is my subsection</title> 
     <para>Text that is inside of the sub-section</para> 
     <para>And some more sub section text.</para> 
    </section> 
</section> 
+0

這工作完美,謝謝! – badperson

+0

+1。正確答案。 – Flack

1

我想這樣做

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

<xsl:key name="kPreceding" match="para" 
    use="generate-id(following-sibling::section[1])"/> 

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

<xsl:template match="section[preceding-sibling::para]"> 
    <comment> 
    <xsl:apply-templates mode="copy" 
     select="key('kPreceding', generate-id())"/> 
    </comment> 

    <xsl:call-template name="identity"/> 
</xsl:template> 

<xsl:template match= 
    "para[following-sibling::section]"/> 

<xsl:template match="para" mode="copy"> 
    <xsl:call-template name="identity"/> 
</xsl:template> 
</xsl:stylesheet> 

當該變換被應用所提供的XML文檔:

<section> 
    <title>Main Section</title> 
    <para>Here is some text that is a child of a main section.</para> 
    <para>Some more text.</para> 
    <para>When a section has subsections, it should not have loose paragraphs before the first sub section. Those loose paras should be placed inside a comment element.</para> 
    <section> 
     <title>This is my subsection</title> 
     <para>Text that is inside of the sub-section</para> 
     <para>And some more sub section text.</para> 
    </section> 
</section> 

有用,正確的結果產生:

<section> 
    <title>Main Section</title> 
    <comment> 
     <para>Here is some text that is a child of a main section.</para> 
     <para>Some more text.</para> 
     <para>When a section has subsections, it should not have loose paragraphs before the first sub section. Those loose paras should be placed inside a comment element.</para> 
    </comment> 
    <section> 
     <title>This is my subsection</title> 
     <para>Text that is inside of the sub-section</para> 
     <para>And some more sub section text.</para> 
    </section> 
</section> 

說明

  1. 的身份規則(模板)每n次複製一次頌歌「原樣」。

  2. section的首要模板具有前述兄弟(一個或多個)包裹para所有這樣的兄弟姐妹與comment元件,然後調用本身的恆等變換。

  3. 爲了方便起見,我們定義了一個鍵,該鍵匹配section元素前面的所有para元素與給定的generate-id()

  4. 具有以下兄弟sectionpara元素通過覆蓋模板排除在身份規則的操作之外,該操作不會執行任何操作。

  5. 最後,這樣para元素,當包裝comment內被輸出在模式copy,這只是簡單地調用身份規則做複製進行處理。

+0

+1這將是另一個可能的樣式表。 – 2011-03-02 12:29:21

相關問題