【发布时间】:2017-03-16 01:44:01
【问题描述】:
您好,我有以下 XML
<?xml version="1.0" encoding="UTF-8"?>
<catalog catalog-id="Primary">
<product product-id="clothes">
<items>
<item item-name="TShirt">Brown</item>
<item item-name="Pants">Green</item>
<item item-name="Shirt">White</item>
</items>
</product>
<product product-id="toys">
<items>
<item item-name="Ball">Cyan</item>
<item item-name="Bat">Green</item>
<item item-name="Racket">Blue</item>
</items>
</product>
</catalog>
标准是仅在“item-name”属性值为 TShirt、Ball 或 Bat 的情况下复制。所以生成的 XML 应该看起来像
<?xml version="1.0" encoding="UTF-8"?>
<catalog catalog-id="Primary">
<product product-id="clothes">
<items>
<item item-name="TShirt">Brown</item>
</items>
</product>
<product product-id="toys">
<items>
<item item-name="Ball">Cyan</item>
<item item-name="Bat">Green</item>
</items>
</product>
</catalog>
我正在使用以下 XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xs:WhiteList>
<item-name>TShirt</item-name>
<item-name>Ball</item-name>
<item-name>Green</item-name>
</xs:WhiteList>
<xsl:variable name="whiteList" select="document('')/*/xs:WhiteList" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[(descendant-or-self::*[name()=$whiteList/item-name])"/>
</xsl:stylesheet>
但这不起作用。你能帮忙吗?
谢谢 内森
【问题讨论】:
-
您的白名单中的
Green是故意的吗?似乎应该是Bat。如果所有Green项目都包含在输出中,那么Pants也将在那里。