使用版本化的xslt转换XML



输入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块对它进行转换。谁来帮帮我

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

<?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>

自从提供了正确答案后,你似乎改变了你的问题;展望未来,请一如既往地提出新问题。无论如何,这里有一个不同的解决方案适合您新需要的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>

<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>

相关内容

  • 没有找到相关文章

最新更新