這種轉變:
<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="kFollowing" match="/*/node()[not(self::nl)]"
use="generate-id(preceding-sibling::nl[1])"/>
<xsl:key name="kPreceding" match="/*/node()[not(self::nl)]"
use="generate-id(following-sibling::nl[1])"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="@*|nl"/>
</xsl:copy>
</xsl:template>
<xsl:template match="nl" name="groupFollowing">
<title>
<xsl:apply-templates select="key('kFollowing',generate-id())"/>
</title>
</xsl:template>
<xsl:template match="nl[1]">
<title>
<xsl:apply-templates select="key('kPreceding',generate-id())"/>
</title>
<xsl:call-template name="groupFollowing"/>
</xsl:template>
</xsl:stylesheet>
時所提供的XML文檔應用:
<p>
XYZZ
<nl/>
DEF
<process> gggg </process>
KKK
<nl/>
JKLK
<nl/>
QQQQ
</p>
產生想要的,正確的結果:
<p>
<title>
XYZZ
</title>
<title>
DEF
<process> gggg </process>
KKK
</title>
<title>
JKLK
</title>
<title>
QQQQ
</title>
</p>
請注意:
身份規則用於複製節點 「原樣」。
有一些特定的模板匹配頂部元件中,第一nl
孩子頂件的任何nl
子頭元素。
兩個鍵中定義的選擇所有非nl
節點緊接-先於nl
元件和緊跟隨nl
元件的所有節點。
的nl
元件由title
元件替換和所有緊接以下非nl
節點被處理,並將結果放入此title
元件。
對於第(其父子)nl
元件有一個初始步驟其中title
元素添加和所有緊接在前非nl
節點被處理,並將結果放入此title
元件。然後執行上述步驟4中的處理。
我需要儘快解決。 – Gracious 2011-01-10 19:29:57
好問題,+1。查看我的答案,獲得最有效的完整簡短解決方案。我仔細解釋了這個解決方案中的所有步驟:) – 2011-01-10 20:08:05