我想变换使用XSLT给定的XML。 需要说明的是,我将不得不删除父节点,如果给定的子节点不存在。 我也做了一些模板匹配,但我坚持。 任何帮助,将不胜感激。
输入的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>