我想要使用xslt转换给定的XML。 需要注意的是,如果给定的子节点不存在,我将不得不删除父节点。 我做了一些模板匹配,但我卡住了。任何帮助,将不胜感激。如果子节点不存在于XML中使用xslt删除父节点
输入XML:
<Cars>
<Car>
<Brand>Nisan</Brand>
<Price>12</Price>
</Car>
<Car>
<Brand>Lawrence</Brand>
</Car>
<Car>
<Brand>Cinrace</Brand>
<Price>14</Price>
</Car>
</Cars>
我想删除不具有内它的价格因素的汽车。 所以预期输出是:
<Cars>
<Car>
<Brand>Nisan</Brand>
<Price>12</Price>
</Car>
<Car>
<Brand>Cinrace</Brand>
<Price>14</Price>
</Car>
</Cars>
我尝试使用这样的:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Cars/Car[contains(Price)='false']"/>
</xsl:stylesheet>
我知道XSLT是完全错误的请指点。
UPDATE
更正其中一个工程:)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<!--Identity template to copy all content by default-->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Car[not(Price)]"/>
</xsl:stylesheet>
+1对于一个好的XSLT问题的所有3个元素。输入,输出,你试过的。 – 2013-02-08 18:42:06