使用版本化的xslt转换xml

时间:2014-05-02 06:51:50

标签: xml xslt xslt-1.0

输入xml

<Carfactory>
 <Table>
  <CarName>Veyron</CarName>
  <Carcode>9196</Carcode>
  <Carprice_1>64760</Carprice_1>
  <Carprice_2>69760</Carprice_2>
  <Carprice_3>64960</Carprice_3>
  <Carprice_4>64790</Carprice_4>
  <Carprice_5>64780</Carprice_5>
  <Carprice_6>64860</Carprice_6>
  .
  .
  <Carprice_27>68760</Carprice_27>
 </Table>
</Carfactory>

输出xml

<Carfactory>
  <CarName>Veyron</CarName>
  <Carcode>9196</Carcode>
  <Carprice>64760</Carprice>
  <count>1</count>
</Carfactory>
<Carfactory>
  <CarName>Veyron</CarName>
  <Carcode>9196</Carcode>
  <Carprice>69760</Carprice>
  <count>2</count>
</Carfactory>
.
.
.
.
<Carfactory>
  <CarName>Veyron</CarName>
  <Carcode>9196</Carcode>
  <Carprice>68760</Carprice>
  <count>27</count>
</Carfactory>

我想使用xslt 1.0获取此输出。我希望使用单个xslt块进行某种循环转换。有人请帮帮我

2 个答案:

答案 0 :(得分:2)

您可以使用以下样式表来实现:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="/">
        <xsl:for-each select="/Carfactory/Table/*[starts-with(name(), 'Carprice')]">
            <Carfactory>
                <xsl:copy-of select="preceding-sibling::CarName"/>
                <xsl:copy-of select="preceding-sibling::Carcode"/>
                <xsl:copy-of select="."/>
            </Carfactory>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

答案 1 :(得分:0)

由于提供了正确答案,您似乎更改了问题;今后,请将新问题作为新问题提出。对于它的价值,这里有一个适合您新的XML的不同解决方案。

当这个XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="no" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/*">
    <t>
      <xsl:apply-templates select="*/*[starts-with(name(), 'Carprice')]"/>
    </t>
  </xsl:template>

  <xsl:template match="*[starts-with(name(), 'Carprice')]">
    <Carfactory>
      <xsl:apply-templates select="../*[self::CarName or self::Carcode]"/>
      <Carprice>
        <xsl:apply-templates/>
      </Carprice>
      <count>
        <xsl:value-of select="substring-after(name(), '_')"/>
      </count>
    </Carfactory>
  </xsl:template>

</xsl:stylesheet>

...适用于提供的XML:

<Carfactory>
  <Table>
    <CarName>Veyron</CarName>
    <Carcode>9196</Carcode>
    <Carprice_1>64760</Carprice_1>
    <Carprice_2>69760</Carprice_2>
    <Carprice_3>64960</Carprice_3>
    <Carprice_4>64790</Carprice_4>
    <Carprice_5>64780</Carprice_5>
    <Carprice_6>64860</Carprice_6>
    <!-- ... -->
    <Carprice_27>68760</Carprice_27>
  </Table>
</Carfactory>

...生成了想要的结果:

<?xml version="1.0" encoding="UTF-8"?>
<t>
  <Carfactory>
    <CarName>Veyron</CarName>
    <Carcode>9196</Carcode>
    <Carprice>64760</Carprice>
    <count>1</count>
  </Carfactory>
  <Carfactory>
    <CarName>Veyron</CarName>
    <Carcode>9196</Carcode>
    <Carprice>69760</Carprice>
    <count>2</count>
  </Carfactory>
  <Carfactory>
    <CarName>Veyron</CarName>
    <Carcode>9196</Carcode>
    <Carprice>64960</Carprice>
    <count>3</count>
  </Carfactory>
  <Carfactory>
    <CarName>Veyron</CarName>
    <Carcode>9196</Carcode>
    <Carprice>64790</Carprice>
    <count>4</count>
  </Carfactory>
  <Carfactory>
    <CarName>Veyron</CarName>
    <Carcode>9196</Carcode>
    <Carprice>64780</Carprice>
    <count>5</count>
  </Carfactory>
  <Carfactory>
    <CarName>Veyron</CarName>
    <Carcode>9196</Carcode>
    <Carprice>64860</Carprice>
    <count>6</count>
  </Carfactory>
  <Carfactory>
    <CarName>Veyron</CarName>
    <Carcode>9196</Carcode>
    <Carprice>68760</Carprice>
    <count>27</count>
  </Carfactory>
</t>