2011-04-28 88 views
2

我從表中取的信息:如何排除XSLT中的字段?

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

我要確保我沒有從該表中包含的字段。那可能嗎?

+0

好問題,+1。查看我的答案,獲取完全基於最基本且功能強大的XSLT設計模式的* complete *和short解決方案:使用和覆蓋身份規則。還提供說明和鏈接。 – 2011-04-28 15:26:25

回答

2

推風格:

<xsl:template match="table1"> 
    <ApplicantAddress> 
    <xsl:apply-templates select="@* | node()[not(self::unwanted-element)]"/> 
    </ApplicantAddress> 
</xsl:template> 

拉風:

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

<xsl:template match="table1/unwanted-element"/> 
+0

所以:' – XstreamINsanity 2011-04-28 15:26:11

+0

非常感謝。 – XstreamINsanity 2011-04-28 15:26:51

+0

@XstreamINsanity:不客氣。 – 2011-04-28 15:31:23

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

<xsl:template match="c"/> 
</xsl:stylesheet> 

當這個XML文檔(你錯過了提供應用):

<table1> 
<a/> 
<b/> 
<c/> 
<d/> 
</table1> 

產生想要的結果(元件b未複製):

<ApplicantAddress> 
    <a /> 
    <b /> 
    <d /> 
</ApplicantAddress> 

說明:使用並覆蓋identity rule /模板是最根本的XSLT設計模式。這裏我們用一個匹配c的空體模板來覆蓋身份規則,這可以確保這個元素被忽略(「刪除」/未被複制)。

+0

我想你的意思是元素'C'吧?這是否與其他答案不同,還是這番茄西紅柿的東西? :) 謝謝。 – XstreamINsanity 2011-04-28 15:32:17

+0

@XstreamINsanity:是的,我的意思是'c',我會編輯我的答案。我的答案與另一個不同,因爲它提供了身份規則(以及要使用的完整樣式表)。 @ Alejandro的回答假設身份規則在樣式表中的某處。他甚至沒有提到這個假設,只是將他的代碼包裝在一個''元素中不會複製'table1'元素的任何子元素 - 只需要嘗試並驗證它。 – 2011-04-28 15:40:27

+0

好的,謝謝。我沒有把所有的東西都放在答案中,只是爲了確保我沒有意外地放置任何專有信息。在我正在編輯的當前XSLT文件中(對XSLT文件是新的)我沒有帶區域,但是我有''。我是否需要strip-space並且必須更改輸出?我也有你提出的第一個模板聲明。 – XstreamINsanity 2011-04-28 15:45:21