2012-11-14 34 views
0

組相同XML項我有XML:更改佈局 - 使用1個元件

<StockInfo> 
<Item> 
<stockcode>111111</stockcode> 
<vehicle>Ford</vehicle> 
<model>Escape (2000-)</model> 
<model>Maverick (2001-)</model> 
<width>8</width> 
<wheel_size>18</wheel_size> 
<offset>35</offset> 
<bolt_pattermn>5x114.3</bolt_pattermn> 
<brand>ANTERA</brand> 
<Velg_ID/> 
<kit1>DK-135259671 x1</kit1> 
<kit2/> 
<kit3/> 
<kit4/> 
<qty_available>3.00000000</qty_available> 
<price>1110.00</price> 
<picture>410110 
</picture> 
</Item> 
<Item> 

<stockcode>111111</stockcode> 
<vehicle>Honda</vehicle> 
<model>Civic (5skr,2001-2006)(2006-)</model> 
<model>Accord (2003-2008)</model> 
<model>Acord Coupe (1999-)</model> 
<model>Acord Type R</model> 
<model>Civic Type R (2001-2006)(2007-,17"&lt;)</model> 
<model>Civic Type S (2001-2006)(2007-,17"&lt;)</model> 
<model>Integra Type R~Prelude (1997-2001)</model> 
<model>Legend (1991-1999)</model> 
<width>8</width> 
<wheel_size>18</wheel_size> 
<offset>40</offset> 
<bolt_pattermn>5x114.3</bolt_pattermn> 
<brand>ANTERA</brand> 
<Velg_ID/> 
<kit1>DK-135259641 x1</kit1> 
<kit2/> 
<kit3/> 
<kit4/> 
<qty_available>3.00000000</qty_available> 
<price>1110.00</price> 
<picture>410110 
</picture> 
</Item> 
<Item> 

<stockcode>2222222</stockcode> 
<vehicle>BMW</vehicle> 
<model>6 (e63/64, 2004-2011)</model> 
<model>M6 (e63/64, 2004-2011)</model> 
<width>9</width> 
<wheel_size>18</wheel_size> 
<offset>15</offset> 
<bolt_pattermn>5x120</bolt_pattermn> 
<brand>AEZ</brand> 
<Velg_ID>AEZ Ares</Velg_ID> 
<kit1>DK-ZJB3 x1</kit1> 
<kit2/> 
<kit3/> 
<kit4/> 
<qty_available>4.00000000</qty_available> 
<price>151110.00</price> 
<picture>41001 
</picture> 
</Item> 
</StockInfo> 

隨着XSLT 1.0我需要組項目成XML使用元素 'stockcode'。到目標XML我不需要重複的元素(對於相同的項目,只有'車輛'和'模型'元素,我需要從其他相同的物品...)

我需要得到這樣的XML:

<StockInfo> 
<Item> 
<stockcode>111111</stockcode> 
<vehicle>Ford</vehicle> 
<model>Escape (2000-)</model> 
<model>Maverick (2001-)</model> 
<vehicle>Honda</vehicle> 
<model>Civic (5skr,2001-2006)(2006-)</model> 
<model>Accord (2003-2008)</model> 
<model>Acord Coupe (1999-)</model> 
<model>Acord Type R</model> 
<model>Civic Type R (2001-2006)(2007-,17"&lt;)</model> 
<model>Civic Type S (2001-2006)(2007-,17"&lt;)</model> 
<model>Integra Type R~Prelude (1997-2001)</model> 
<model>Legend (1991-1999)</model> 
<width>8</width> 
<wheel_size>18</wheel_size> 
<offset>40</offset> 
<bolt_pattermn>5x114.3</bolt_pattermn> 
<brand>ANTERA</brand> 
<Velg_ID/> 
<kit1>DK-135259641 x1</kit1> 
<kit2/> 
<kit3/> 
<kit4/> 
<qty_available>3.00000000</qty_available> 
<price>1110.00</price> 
<picture>410110</picture> 
</Item> 
<Item> 

<stockcode>2222222</stockcode> 
<vehicle>BMW</vehicle> 
<model>6 (e63/64, 2004-2011)</model> 
<model>M6 (e63/64, 2004-2011)</model> 
<width>9</width> 
<wheel_size>18</wheel_size> 
<offset>15</offset> 
<bolt_pattermn>5x120</bolt_pattermn> 
<brand>AEZ</brand> 
<Velg_ID>AEZ Ares</Velg_ID> 
<kit1>DK-ZJB3 x1</kit1> 
<kit2/> 
<kit3/> 
<kit4/> 
<qty_available>4.00000000</qty_available> 
<price>151110.00</price> 
<picture>41001</picture> 
</Item> 
</StockInfo> 
+0

你試過嗎? – vels4j

回答

0

可以在xslt1.0做分組

<xsl:key name="stockKey" match="StockInfo/Item" use="stockcode"/> 
<xsl:for-each select="/StockInfo/Item 
     [generate-id() = generate-id(key('stockKey',stockcode))]"> 
      <xsl:value-of select="stockcode"/> 
</xsl:for-each> 

也可參考此XSLT Grouping and Sorting Example (Muenchian Method)

0

我覺得我做錯了什麼......翻翻論壇,TU torials,試圖讓工作這樣的代碼:

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

<xsl:key name="stcode" match="StockInfo/Item" use="stockcode" /> 

<xsl:template match="Item" mode="group"> 
<xsl:for-each select="/StockInfo/Item[generate-id() = generate-id(key('stcode',stockcode))]"> 
<xsl:value-of select="stockcode"/> 
<xsl:value-of select="vehicle"/> 
<xsl:value-of select="model"/> 
... 
<xsl:value-of select="other_elements"/> 
</xsl:for-each> 
</xsl:template> 
</xsl:stylesheet> 

但它返回相同的XML源XML ...

+0

這不是一個傳統的論壇;因此,你應該避免發佈猜測等作爲單獨的答案。這可以更好地用作對原始問題的評論。 – ABach