我有这个xml:
<?xml version="1.0" encoding="utf-8"?>
<xml>
<head>
<name>This is my XML</name>
<version>This is my XML</version>
</head>
<body>
<item id="SH2-99435">
<properties>
<property id="69">
<name>This is a property</name>
<amount>54.13</amount>
<estructure id="IZ4">
<name>caterpillar</name>
<location><zipCode>02210</zipCode><street>South Station</street></location>
</estructure>
</property>
<features id="ABC3">
<feature>If it works, a bug is another feature.</feature>
<feature>feature!!</feature>
</features>
</properties>
<coding>
<codename>Silent Hill 2</codename>
<developer>Team Silent</developer>
<publisher>Konami</publisher>
</coding>
</item>
<item id="SH3-4498">
<text value="it has values like the other item"/>
</item>
<item id="MGS-2">
<text value="it has values like the other item"/>
</item>
</body>
</xml>
我想实现这一点:
<?xml version="1.0" encoding="utf-8"?>
<xml>
<head>
<name>This is my XML</name>
<version>This is my XML</version>
</head>
<body>
<item>
<properties>
<property id="69">
<amount>54.13</amount>
<estructure id="IZ4">
<name>caterpillar</name>
<location><zipCode>02210</zipCode><street>South Station</street></location>
</estructure>
</property>
</properties>
</item>
<item id="SH3-4498">
<text value="it should have properties and the selected sons"/>
</item>
<item id="MGS-2" >
<text value="it should have properties and the selected sons"/>
</item>
</body>
</xml>
我有这样的东西,可以正确过滤:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="xml/body/item/coding" />
<xsl:template match="xml/body/item/properties/features" />
<xsl:template match="xml/body/item/properties/property/name" />
</xsl:stylesheet>
有人告诉我,运行此过滤器将生成 10 个文件,每个文件都有不同的标签;但如果出现新标签,我们将不得不更改 10 个文件以排除不需要的标签,而不是只在需要的文件上添加标签。例如,在另一个文件中,只包含编码,等等。
我试过这个:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="some:ns">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*" />
<ns:WhiteList>
<name>amount</name>
<name>estructure</name>
</ns:WhiteList>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template
match="*[not(descendant-or-self::*[name()=document('')/*/ns:WhiteList/*])]" />
</xsl:stylesheet>
但它不会复制 estructure 的子代。你知道我能做什么吗?
谢谢。
更新。增加了这样做的理由,而不是相反,以及更具描述性的问题。