2014-05-02 73 views
0

輸入XML版本使用XSLT轉換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.I得到這個輸出需要某種形式的循環利用改造它的一個單一的xslt塊。有人請幫我出來

+0

您需要什麼樣的幫助? –

+0

還請注意,由於缺少根元素,請求的輸出無效XMl。 –

回答

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

當然......謝謝.. – user3595236

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

我在輸出中添加了另一個標籤數量,你可以幫我解決這個問題 – user3595236