将XML转换为不同的布局

时间:2012-07-17 23:36:53

标签: xml transform

嗨,谢谢你提前

给定此输入XML

<root>
<part id='74'>
    <title>Belt</title>
    <description>1158J5</description>
    <partno>C00112576</partno>
    <price>10.99</price>
</part>
<category id='6' name='Washer-Dryer'></category>
<category id='2' name='Washing machine'></category>
<make id='6' name='Ariston'></make>
<make id='2' name='Hotpoint'></make>
<make id='33' name='Indesit'></make>
<model catid='2' catname='Washing machine' makeid='6' makename='Ariston' modelid='524'>A1235</model>
<model catid='2' catname='Washing machine' makeid='2' makename='Hotpoint' modelid='413'>SCR37P</model>
<model catid='2' catname='Washing machine' makeid='2' makename='Hotpoint' modelid='35751'>WD440</model>
<model catid='6' catname='Washer-Dryer' makeid='33' makename='Indesit' modelid='2989'>WF000G</model>

我想将其转换为

<root>
<part id='74'>
    <title>Belt</title>
    <description>1158J5</description>
    <partno>C00112576</partno>
    <price>10.99</price>
</part>
<category id='6' name='Washer-Dryer'>
    <make id='33' name='Indesit'>
        <model catid='6' catname='Washer-Dryer' makeid='33' makename='Indesit' modelid='2989'>WF000G</model>
    </make>
</category>
<category id='2' name='Washing machine'>
    <make id='6' name='Ariston'>
        <model catid='2' catname='Washing machine' makeid='6' makename='Ariston' modelid='524'>A1235</model>
    </make>
    <make id='2' name='Hotpoint'>
        <model catid='2' catname='Washing machine' makeid='2' makename='Hotpoint' modelid='413'>SCR37P</model>
        <model catid='2' catname='Washing machine' makeid='2' makename='Hotpoint' modelid='35751'>WD440</model>
    </make>
</category>

我从asp经典Feed中收到这个XML,我可以访问asp并且可以在那里进行,一旦收到我在php中,我就会发送到xsl转换输出到浏览器

所以我的问题是哪种方式最好的方式,即asp,php或xsl,并且至少可以在最好的方式上启动我

许多人再次感谢 蒂姆·道奇森

1 个答案:

答案 0 :(得分:0)

谷歌搜索和拔出头发的好日子,我想出了这个

<xsl:template match="root">
    <xsl:for-each select="//make">
        <xsl:variable name="makeid" select="@id" />
        <xsl:variable name="makename" select="@name" />
        <make id="{$makeid}" name="{$makename}">
        <xsl:for-each select="//model[@makeid = $makeid]">
            <model catid="{@catid}" catname="{@catname}" makeid="{@makeid}" makename="{@makename}" modelid="{@modelid}">
                <xsl:value-of select="."/>
            </model>
        </xsl:for-each>
        </make>
    </xsl:for-each> 
</xsl:template>

我知道它起初不是我想要的,但它可以满足我的需求。只是希望有人可以说我是否至少以正确的方式前进

很多谢谢蒂姆