2010-05-26 67 views
0

我已經查找了有關stackflow的解決方案,但它們都不適用於我。這是我的問題。可以說我有以下文字:在XSLT期間保留某些html標記

來源:

<greatgrandparent> 
<grandparent> 
    <parent> 
     <sibling> 
       Hey, im the sibling . 
      </sibling> 
     <description> 
     $300$ <br/> $250 <br/> $200! <br/> <p> Yes, that is right! <br/> You can own a ps3 for only $200 </p> 
     </description> 
    </parent> 
    <parent> 
     ... (SAME FORMAT) 
    </parent> 
     ... (Several more parents) 
</grandparent> 
</greatgrandparent> 

輸出:

<newprice> 
     $300$ <br/> $250 <br/> $200! <br/> Yes, that is right! <br/> You can own a ps3 for only $200 
    </newprice> 

我似乎無法找到一個方法來做到這一點。

當前XSL:

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

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

    <xsl:template match = "grandparent"> 

    <xsl:for-each select = "parent" > 
      <newprice> 
      <xsl:apply-templates> 
      </newprice> 
    </xsl:for-each> 
    </xsl:template> 

<xsl:template match="description"> 
    <xsl:element name="newprice"> 
     <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 

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

回答

5

使用模板來對特定元素

<!-- after standard identity template --> 

<xsl:template match="description"> 
    <xsl:element name="newprice"> 
     <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 

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

第一個模板說,與newprice交換description定義行爲。第二個說要忽略p元素。

如果您不熟悉身份模板take a look here的幾個示例。

編輯:給定新的例子,我們可以看到你只想提取描述元素及其內容。注意模板動作以match="/"模板開始。我們可以在我們的樣式表開始的地方使用這個控件,從而跳過我們想要過濾掉的大部分痞子。

變化<xsl:template match="/">到更多的東西,如:

<xsl:template match="/"> 
     <xsl:apply-templates select="//description"/> 
     <!-- use a more specific XPath if you can --> 
    </xsl:template> 

所以乾脆我們的解決方案是這樣的:

<xsl:stylesheet 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" 
    exclude-result-prefixes="xs"> 

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

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

<xsl:template match="description"> 
    <xsl:element name="newprice"> 
     <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 

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

</xsl:stylesheet> 
+0

我會試試這個。我會讓你知道它是怎麼回事。我認爲我遇到了一個的問題,這是因爲一些奇怪的原因,在其中添加了其他字段。感謝您的迴應! – Bilzac 2010-05-26 14:31:35

+0

使用身份模板是'的關鍵''如果您需要一個完整的示例或足夠的話,請告訴我。 – 2010-05-26 14:38:18

+0

呃。我無法讓它工作。它似乎是添加了所有描述的兄弟姐妹。 – Bilzac 2010-05-26 14:55:37

0

不應該是一個CDATA元素中的內容是什麼?然後可能禁用輸出編碼xsl:value-of ..

+0

感謝您的答覆!我沒有被告知他們需要成爲我主管的CDTATA的一部分。 – Bilzac 2010-05-26 14:31:54

0

你應該看看xsl:copy-of

你可能會風與財產以後這樣的:

<xsl:template match="description"> 
    <xsl:copy-of select="."/> 
</xsl:template> 
+0

感謝您的迴應! – Bilzac 2010-05-26 14:31:35

0

可能最短的解決方案是這樣的一個

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

<xsl:template match="description"> 
    <newprice> 
    <xsl:copy-of select="node()"/> 
    </newprice> 
</xsl:template> 

<xsl:template match="text()[not(ancestor::description)]"/> 
</xsl:stylesheet> 

當所提供的XML文檔應用這種轉變,想要的結果產生:

<newprice> 
     $300$ <br /> $250 <br /> $200! <br /> <p> Yes, that is right! <br /> You can own a ps3 for only $200 </p> 
     </newprice> 

請注意

  1. 採用<xsl:copy-of select="node()"/>複製所有的子樹植根在description,沒有根本身。

  2. 我們如何覆蓋(與特定的,模板)內置模板 XSLT,防止不屬於<description>元素的後代任何文本節點,輸出。

+0

感謝您的迴應! – Bilzac 2010-05-26 17:50:07