2013-03-24 42 views
0

我試圖搞清楚XSL如何結束元素<大膽>它元素的父末父標籤

編輯此外,元素<款>和<大膽>的內容得到了<風格名稱 =「粗體」>。排除內容<鏈接>元素。 <款式>將包裝在<段落的內容>和<加粗>。此外元素<款>的內容可以有一個或多個<大膽>和<鏈接>



輸入XML

<paragraph> 
    This is some text that has no style 
</paragraph> 

<paragraph> 
    This is some text that is <bold>correct way</bold> <link>need  
    to be linked </link> to a document 
</paragraph> 

<paragraph> 
    This is some text that is <bold>incorrect <link>need 
    to be linked </link> way </bold> to a document 
</paragraph> 


輸出XML應該

<paragraph> 
    This is some text that has no style 
</paragraph> 

<paragraph> 
    <style name="bold">This is some text that is <bold>correct way</bold></style> 
    <link>need to be linked </link> 
    <style name="bold"> to a document</style> 
</paragraph> 

<paragraph> 
    <style name="bold">This is some text that is <bold>incorrect</bold></style> 
    <link>need to be linked </link> 
    <style name="bold"><bold> way </bold> to a document</style> 
</paragraph> 

任何幫助,非常感謝。

回答

0

該轉化

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

<xsl:template match="bold[link]"><xsl:apply-templates/></xsl:template> 

<xsl:template match="bold[link]/text()"> 
    <bold><xsl:value-of select="."/></bold> 
</xsl:template> 
</xsl:stylesheet> 

當施加到所提供的XML文檔(所提供的片段包裹成一個單一的頂部元件):

<t> 
<paragraph> 
    This is some text that is <bold>correct way</bold> <link>need 
    to be linked </link> to a document 
</paragraph> 

<paragraph> 
    This is some text that is <bold>incorrect <link>need 
    to be linked </link> way </bold> to a document 
</paragraph> 
</t> 

產生想要的,正確結果:

<t> 
    <paragraph> 
    This is some text that is <bold>correct way</bold> 
     <link>need 
    to be linked </link> to a document 
</paragraph> 
    <paragraph> 
    This is some text that is <bold>incorrect </bold> 
     <link>need 
    to be linked </link> 
     <bold> way </bold> to a document 
</paragraph> 
</t> 

更新問題上的OP - 這裏是稍微修改後的轉換實現初始+新要求

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

<xsl:template match="paragraph[bold]"> 
    <paragraph> 
    <style name="bold"><xsl:apply-templates/></style> 
    </paragraph> 
</xsl:template> 

<xsl:template match="bold[link]"><xsl:apply-templates/></xsl:template> 

<xsl:template match="bold[link]/text()"> 
    <bold><xsl:value-of select="."/></bold> 
</xsl:template> 
</xsl:stylesheet> 

當這種轉換應用於以下文件:

<t> 
<paragraph> 
    This is some text that has no style 
</paragraph> 

<paragraph> 
    This is some text that is <bold>correct way</bold> <link>need 
    to be linked </link> to a document 
</paragraph> 

<paragraph> 
    This is some text that is <bold>incorrect <link>need 
    to be linked </link> way </bold> to a document 
</paragraph> 
</t> 

想要的,正確的結果是親duced

<t> 
    <paragraph> 
    This is some text that has no style 
</paragraph> 
    <paragraph> 
     <style name="bold"> 
    This is some text that is <bold>correct way</bold> 
     <link>need 
    to be linked </link> to a document 
</style> 
    </paragraph> 
    <paragraph> 
     <style name="bold"> 
    This is some text that is <bold>incorrect </bold> 
     <link>need 
    to be linked </link> 
     <bold> way </bold> to a document 
</style> 
    </paragraph> 
</t> 
+0

謝謝,我編輯的問題 – user2204573 2013-03-24 18:19:21

+0

@ user2204573,請參閱更新這個答案。 – 2013-03-24 18:55:42

+0

再次感謝。 LINK元素不應該被包裝在STYLE元素之間。 PARAGRAPH元素在PARAGRAPH和BOLD元素的內容中應該有STYLE元素,但NOT LINK元素 – user2204573 2013-03-25 01:20:09