سؤال

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

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

I want to get this output using xslt 1.0.I want some kind of looping to transform it using a single xslt block.Someone please help me out

هل كانت مفيدة؟

المحلول 2

It appears that you changed your question since the correct answer was provided; going forward, please ask new questions as new questions. For what it's worth, here's a different solution that fits your newly desired XML.

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

...is applied against the provided 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>

...the wanted result is produced:

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

نصائح أخرى

you can achieve that using the following stylesheet:

<?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>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top