使用XSLT来减少XML输出 [英] use XSLT to pare down XML output

查看:135
本文介绍了使用XSLT来减少XML输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用XSLT,只选择一些xml标签从我的输入xml到我的输出XML?
示例输入:

How can I using XSLT, select only some xml tags from my input xml to my output XML? example input:

<Country value="USA">
    <State value="KY>
        <City value="Hebron" />
        <City value="Lexington" />
        <City value="Owensboro" />
        <City value="Jonesville" />
    </State>
    <State value="OH">
         <City value="Cincinnati" />
         <City value="Columbus" />
         <City value="Cleveland" />
         <City value="Jonesville" />
    </State>
    <State value="IN" >
         <City value="Indianapolis" />
    </State>
 </Country>

只需复制Hebron和Cincinnati?

So, keep the Country/State tags in place and only copy Hebron and Cincinnati?

预期输出:

<Country value="USA">
    <State value="KY>
        <City value="Hebron" />
    </State>
    <State value="OH">
         <City value="Cincinnati" />
    </State>
 </Country>


推荐答案

以下样式表:

<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="City[not(@value='Hebron' or @value='Cincinnati')]" />
</xsl:stylesheet>

在此输入:

<Country value="USA">
    <State value="KY">
        <City value="Hebron" />
        <City value="Lexington" />
        <City value="Owensboro" />
    </State>
    <State value="OH">
        <City value="Cincinnati" />
        <City value="Columbus" />
        <City value="Cleveland" />
    </State>
</Country>

产生以下结果:

<Country value="USA">
    <State value="KY">
        <City value="Hebron" />
    </State>
    <State value="OH">
        <City value="Cincinnati" />
    </State>
</Country>

此样式表使用

This stylesheet uses the identity transform to copy all but the undesired nodes to the output unchanged.

另一个例子:身份变换 =nofollow

Another example

您可能还想删除任何没有所需城市的元素。此样式表:

You might also want to remove any State element that does not have a desired city. This stylesheet:

<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="City[not(@value='Hebron' or @value='Cincinnati')]"/>
    <xsl:template 
           match="State[not(City[@value='Hebron' or @value='Cincinnati'])]"/>
</xsl:stylesheet>

应用于此输入:

/ p>

<Country value="USA">
    <State value="KY">
        <City value="Hebron" />
        <City value="Lexington" />
        <City value="Owensboro" />
    </State>
    <State value="OH">
        <City value="Cincinnati" />
        <City value="Columbus" />
        <City value="Cleveland" />
    </State>
    <State value="MO">
        <City value="St. Louis" />
    </State>
</Country>

产生:

<Country value="USA">
    <State value="KY">
        <City value="Hebron" />
    </State>
    <State value="OH">
        <City value="Cincinnati" />
    </State>
</Country>

这篇关于使用XSLT来减少XML输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆