2010-11-18 78 views
3

我有下面的XML代碼XSLT處裂開子節點

<para>Lorem ipsum <link>dolor</link> sit amet</para> 

,我想轉換到

<para>Lorem ipsum </para><link>dolor</link><para> sit amet</para> 

換句話說:我想para元素的位置進行分割鏈接元素在哪裏。有沒有提示?

+0

好問題,+1。看到我的答案是一個完整的,非常簡短的解決方案。 :) – 2010-11-18 16:28:00

回答

0

這個樣式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()[1]"/> 
     </xsl:copy> 
     <xsl:apply-templates select="following-sibling::node()[1]"/> 
    </xsl:template> 
    <xsl:template match="para"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()[1]"/> 
     </xsl:copy> 
     <xsl:apply-templates select="link" mode="copy"/> 
     <xsl:apply-templates select="following-sibling::node()[1]"/> 
    </xsl:template> 
    <xsl:template match="para/link"/> 
    <xsl:template match="para/link" mode="copy"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()[1]"/> 
     </xsl:copy> 
     <para> 
      <xsl:apply-templates select="following-sibling::node()[1]"/> 
     </para> 
    </xsl:template> 
</xsl:stylesheet> 

輸出:

<para>Lorem ipsum </para><link>dolor</link><para> sit amet</para> 

注意:細粒度遍歷。

編輯:緊湊代碼:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="node()|@*" name="identity"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()[1]"/> 
     </xsl:copy> 
     <xsl:apply-templates select="self::para/link" mode="copy"/> 
     <xsl:apply-templates select="following-sibling::node()[1]"/> 
    </xsl:template> 
    <xsl:template match="para/link"/> 
    <xsl:template match="para/link" mode="copy"> 
     <xsl:call-template name="identity"/> 
    </xsl:template> 
    <xsl:template match="node()[preceding-sibling::node()[1] 
            /self::link/parent::para]"> 
     <para> 
      <xsl:call-template name="identity"/> 
     </para> 
    </xsl:template> 
</xsl:stylesheet> 
+0

節點'para'被複制而不是生成?如果它具有屬性呢? Bruno 2018-03-05 01:37:40

2

該轉化

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

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

<xsl:template match="/*"> 
    <xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="para/text()"> 
    <para><xsl:copy-of select="."/></para> 
</xsl:template> 
</xsl:stylesheet> 

當所提供的XML文檔施加:

<para>Lorem ipsum <link>dolor</link> sit amet</para> 

產生想要的,正確的結果

<para>Lorem ipsum </para><link>dolor</link><para> sit amet</para> 

請注意

  1. 使用身份規則的每一個節點複製原樣。

  2. 身份規則的帶有模板的首要用於處理僅特定節點

  3. 極其簡單性和功率從使用1和2的上方跟隨。

+0

嗨Dimitre,非常感謝您提供了非常優雅的解決方案,但我有一個問題,的內容模型也允許其他元素,它不應該像鏈接應該結束段落。所以我不得不使用Alejandros的建議。 – Roman 2010-11-22 09:05:19

+0

@Roman:您可以編輯您的問題並顯示您的XML的真實結構,然後確定我會修改我的答案以匹配我尚未見過的XML結構。這裏沒有人是clairevoyant。 :) – 2010-11-22 13:37:33