[英]XML output generating using XSLT
I have an XML source file as an input which is the following: 我有一个XML源文件作为输入,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<car>
<brand>Mercedes</brand>
<type>ClassA</type>
<engine>Diesel</engine>
<seats>5</seats>
</car>
<car>
<brand>Audi</brand>
<type>A8</type>
<engine>Diesel</engine>
<seats>2</seats>
</car>
<car>
<brand>Mercedes</brand>
<type>ClassB</type>
<engine>Petrol</engine>
<seats>5</seats>
</car>
</catalog>
I need to filter the cars using an .xsl styleshett (XSLT) according to various data (for example: I want the list of the Mercedes brand cars with all of it's properties). 我需要根据各种数据使用.xsl styleshett(XSLT)过滤汽车(例如:我想要具有所有属性的梅赛德斯品牌汽车的列表)。 My output file's structure (XML tags) has to be the same as the input after the filter.
我的输出文件的结构(XML标记)必须与过滤器后的输入相同。
In this case (filter Mercedes brand cars) the output has to be: 在这种情况下(过滤奔驰品牌的汽车),输出必须为:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<car>
<brand>Mercedes</brand>
<type>ClassA</type>
<engine>Diesel</engine>
<seats>5</seats>
</car>
<car>
<brand>Mercedes</brand>
<type>ClassB</type>
<engine>Petrol</engine>
<seats>5</seats>
</car>
</catalog>
You can simply filter out any non-Mercedes brand
cars with 您只需使用以下方法过滤掉所有非奔驰
brand
汽车
<xsl:template match="car[brand!='Mercedes']" />
Combine this with the identity template to copy all remaining nodes: 将此与身份模板结合起来以复制所有剩余节点:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
You can reverse the approach by explicitly excluding certain brands (here 'Audi' and 'Ford') and keeping all the others with 您可以通过明确排除某些品牌(此处为“ Audi”和“ Ford”)并保留所有其他品牌来扭转这种做法
<xsl:template match="car[brand='Audi' or brand='Ford']" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="catalog">
<xsl:element name="catalog">
<xsl:for-each select="//car">
<xsl:choose>
<xsl:when test=".[brand='Audi']"/>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:element>
</xsl:template>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.