使用 XSLT 过滤 XML 标签 [英] filtering XML tags using XSLT
问题描述
我有以下 XML 代码.
I have the following XML code.
您会注意到标签说明重复了,但具有不同的属性.
You will notice the tag Description is repeated, but with different attributes.
我正在使用 XSLT 尝试删除具有启用属性的描述标记.
I am using XSLT to try and remove the Description tag with the enabled attribute.
<Batch>
- <Promotion>
<LastUpdated>2008-01-22T11:58:05+00:00</LastUpdated>
<MajorVersion>1</MajorVersion>
<MinorVersion>29</MinorVersion>
<PromotionID>000873</PromotionID>
<Description enabled="1">*P* Free Mistletoe</Description>
<Description country="GB" language="en" variant="">WANTED LINE 1</Description>
</Promotion>
<Promotion>
<LastUpdated>2008-01-22T11:58:05+00:00</LastUpdated>
<MajorVersion>1</MajorVersion>
<MinorVersion>29</MinorVersion>
<PromotionID>000874</PromotionID>
<Description enabled="1">*P* Free Mistletoe</Description>
<Description country="GB" language="en" variant="">WANTED LINE 2</Description>
</Promotion>
</batch>
这就是我想要达到的,还有其他标签,它是删除一个基于我正在尝试解析的属性的行.
This is what I am trying to get to, there are other tags, it is the removal of one line based on an attribute I am trying to resolve.
- <promotions>
- <promotion>
<promotionID>000873</promotionID>
<description country="GB" language="en" variant="">WANTED LINE 1</description>
</promotion>
- <promotion>
<promotionID>000874</promotionID>
<description country="GB" language="en" variant="">WANTED LINE 2</description>
</promotion>
</promotions>
我使用的 XSLT 代码是
The XSLT code I am using is
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//promotion/Description[@country='GB']"/>
<xsl:template match="/">
<promotions>
<xsl:for-each select="Batch/Promotion">
<promotion>
<promotion_id><xsl:value-of select="PromotionID"/></promotion_id>
<description><xsl:value-of select="Description"/></description>
</promotion>
</xsl:for-each>
</promotions>
</xsl:template>
</xsl:stylesheet>
如果有人能指出我正确的方向,我将不胜感激.
If anyone could point me in the right direction I would be very grateful.
保罗
推荐答案
一般来说,为了移除一个元素,你必须指定一个没有任何内容的模板.就您而言,这可能是:
Generally, in order to remove an element, you have to specify a template without any contents. In your case, this could be:
<xsl:template match="/Batch/Promotion/Description[@enabled = '1']"/>
然而,在您的 XSLT 代码中,您需要构建自己的 <description>
元素的特殊情况.为了准确获得所需
元素的值,请在
元素中选择它:
In your XSLT code, however, you have the somewhat special case of constructing your own <description>
element. In order to get exactly the value of the desired <Description>
element, select it in your <xsl:value-of>
element:
<description><xsl:value-of select="Description[@country = 'GB']"/></description>
这就是您在问题中所描述的,但是您的预期结果代码意味着您已经想要复制
元素的属性?在这种情况下,我会使用 <xsl:copy-of>
:
This is what you have described in your question, however your expected result code implies that you already want to copy the attributes of the <Description>
element? In that case, I'd go for this solution with <xsl:copy-of>
:
<description><xsl:copy-of select="Description[@country = 'GB']/node()|Description[@country = 'GB']/@*"/></description>
它复制
元素(node()
)的全部内容,以及它的任何属性(@*
)代码>).
It copies the whole contents of the <Description>
element (node()
), as well as any of its attributes (@*
).
这篇关于使用 XSLT 过滤 XML 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!