简体   繁体   English

使用xsl进行XML转换-每个

[英]XML transformation with xsl - for each

I have a xml and I want to transform it into another xml. 我有一个xml,我想将其转换为另一个xml。 Looking for 2 days and have not found any good example for my case; 找了两天,还没有找到适合我的案例的好例子; 2 nodes cars and distances . 2个节点的汽车距离

For each id_car, I have to group the distances (see below the output xml). 对于每个id_car,我必须将距离分组(请参见下面的输出xml)。

Source : 资源 :

<?xml version="1.0" encoding="ISO-8859-1"?>
<output>
    <cars>
        <car>
            <id>1</id>
            <brand>Audi</brand>
            <type>A4_Quattro</type>
            <license>TEST</license>
        </car>
        <car>
            <id>2</id>
            <brand>FORD</brand>
            <type>XLT_Ranger</type>
            <license>PROTOTYPE</license>
        </car>
    </cars>
    <distances>
        <distance>
            <id_car>1</id_car>
            <date>20110901</date>
            <distance>123</distance>
        </distance>
        <distance>
            <id_car>1</id_car>
            <date>20110902</date>
            <distance>194</distance>
        </distance>
        <distance>
            <id_car>2</id_car>
            <date>20110907</date>
            <distance>24</distance>
        </distance>
        <distance>
            <id_car>2</id_car>
            <date>20110915</date>
            <distance>105</distance>
        </distance>
    </distances>
</output>  

output xml: 输出xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<output>
    <cars>
        <car>
            <id>1</id>
            <brand>Audi</brand>
            <type>A4_Quattro</type>
            <distances> 
                       <distance day="20110901">123</distance>
                       <distance day="20110902">194</distance>
            </distances> 
        </car>
        <car>
            <id>2</id>
            <brand>FORD</brand>
            <type>XLT_Ranger</type>
            <license>PROTOTYPE</license>
            <distances> 
                       <distance day="20110907">24</distance>
                       <distance day="20110915">105</distance>
            </distances>
        </car>
    </cars>
<output>

This is the result of dozens of attempts: 这是数十次尝试的结果:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="Dist_car" match="distances/distance" use="id_car" />
<xsl:template match="@*|node()">
<xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <distances>
        <xsl:apply-templates select="key('Dist_car', id)"/>
        </distances>
 </xsl:copy>
 </xsl:template>
        <xsl:template match="distance">
        <distance day="{date}"><xsl:value-of select="distance"/></distance>
 </xsl:template>
</xsl:stylesheet>

If anyone has any idea is welcome ...thanks a lot! 如果有人有任何想法,欢迎...非常感谢!

PS : i test the xsl with this one http://xslttest.appspot.com/ . PS:我用这个http://xslttest.appspot.com/测试xsl。

Your template matches too broadly, so it generates too many <distances> elements and it also copies the original distances. 您的模板匹配范围太广,因此它会生成太多<distances>元素,并且还会复制原始距离。

Try to narrow down the match rules. 尝试缩小匹配规则。 You can also copy sections of the source document using xsl:copy-of . 您还可以使用xsl:copy-of源文档的各个部分。

I'm getting your desired output if I change your stylesheet a bit to: 如果将样式表稍微更改为以下内容,则会得到所需的输出:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:key name="Dist_car" match="distances/distance" use="id_car" />
    <xsl:template match="/">
        <output>
            <cars>
                <xsl:apply-templates select="output/cars" />
            </cars>
        </output>
    </xsl:template>
    <xsl:template match="car">
        <xsl:copy>
            <xsl:copy-of select="@*|node()" />
            <distances>
                <xsl:apply-templates select="key('Dist_car', id)" />
            </distances>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="distance">
        <distance day="{date}">
            <xsl:value-of select="distance" />
        </distance>
    </xsl:template>
</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM