我有以下XML示例
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="urn:com:ssn:schema:export:SSNExportFormat.xsd" Version="0.1" DocumentID="7bf15843-4fe7-4692-b638-b42ada6b86f6-2" ExportID="7bf15843-4fe7-4692-b638-b42ada6b86f6" JobID="132251" RunID="7238837" CreationTime="2017-03-09T04:00:08.209+13:00" StartTime="2017-03-08T01:00:00.000+13:00" EndTime="2017-03-09T01:00:00.000+13:00">
<product product-id="clothes">
<items>
<item item-name="TShirt" size="L">Brown</item>
<item item-name="Pants" size="L">Green</item>
<item item-name="Shirt" size="L">White</item>
</items>
</product>
<product product-id="toys">
<items>
<item item-name="Ball" size="L">Cyan</item>
<item item-name="Bat" size="L">Green</item>
<item item-name="Racket" size="L">Blue</item>
</items>
</product>
</catalog>
标准是仅复制“item-name”属性值为TShirt,Ball和Bat的位置(即:还有更多元素,但我们只使用白名单)。生成的XML应该像
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="urn:com:ssn:schema:export:SSNExportFormat.xsd" Version="0.1" DocumentID="7bf15843-4fe7-4692-b638-b42ada6b86f6-2" ExportID="7bf15843-4fe7-4692-b638-b42ada6b86f6" JobID="132251" RunID="7238837" CreationTime="2017-03-09T04:00:08.209+13:00" StartTime="2017-03-08T01:00:00.000+13:00" EndTime="2017-03-09T01:00:00.000+13:00">
<product product-id="clothes">
<items>
<item item-name="TShirt" size="L">Brown</item>
</items>
</product>
<product product-id="toys">
<items>
<item item-name="Ball" size="L">Cyan</item>
<item item-name="Bat" size="L">Green</item>
</items>
</product>
</catalog>
以下XSL不起作用
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="urn:com:ssn:schema:export:SSNExportFormat.xsd"
xmlns:local="local">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<local:WhiteList>
<item-name>TShirt</item-name>
<item-name>Pants</item-name>
<item-name>Ball</item-name>
</local:WhiteList>
<xsl:variable name="whiteList" select="document('')/*/local:WhiteList/item-name"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="item">
<xsl:if test="@item-name=$whiteList">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
如果从XML中删除了“urn:com:ssn:schema:export:SSNExportFormat.xsd”的默认命名空间,则XSLT可以正常工作。你能帮忙吗?
答案 0 :(得分:1)
如果输入文档具有默认名称空间,则相关样式表应使用名称空间前缀声明它。
修改后的样式表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ssne="urn:com:ssn:schema:export:SSNExportFormat.xsd"
xmlns:local="local">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<local:WhiteList>
<item-name>TShirt</item-name>
<item-name>Pants</item-name>
<item-name>Ball</item-name>
</local:WhiteList>
<xsl:variable name="whiteList" select="document('')/*/local:WhiteList/item-name"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ssne:item">
<xsl:if test="@item-name = $whiteList">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
结果:
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="urn:com:ssn:schema:export:SSNExportFormat.xsd"
Version="0.1"
DocumentID="7bf15843-4fe7-4692-b638-b42ada6b86f6-2"
ExportID="7bf15843-4fe7-4692-b638-b42ada6b86f6"
JobID="132251"
RunID="7238837"
CreationTime="2017-03-09T04:00:08.209+13:00"
StartTime="2017-03-08T01:00:00.000+13:00"
EndTime="2017-03-09T01:00:00.000+13:00">
<product product-id="clothes">
<items>
<item item-name="TShirt" size="L">Brown</item>
<item item-name="Pants" size="L">Green</item>
</items>
</product>
<product product-id="toys">
<items>
<item item-name="Ball" size="L">Cyan</item>
</items>
</product>
</catalog>