2011-04-28 32 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