2012-11-13 84 views
1

我有這樣的XML:更改XML的佈局 - 將XML元素值拆分爲多個元素;除去不必要的元素

<StockInfo> 
<Item> 
<stockcode>111111</stockcode> 
<vehicle>Ford</vehicle> 
<model>Escape (2000-)~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-)~Accord (2003-2008)~Acord Coupe (1999-)~Acord Type R~Civic Type R (2001-2006)(2007-,17"&lt;)~Civic Type S (2001-2006)(2007-,17"&lt;)~Integra Type R~Prelude (1997-2001)~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)~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> 

正如所看到的有2相同的項(具有相同的stockcode;產品車車輪)和1個不同的項目...相同的項可以是X次(如果這個車輪適用於奧迪A3,奧迪A4,雪鐵龍X,它將成爲XML的3倍,所有其他元素(除了並具有相同的值))。

我的問題:

  1. 如何分離/分割元件<model>

    <model>Civic (5skr,2001-2006)(2006-)~Accord (2003-2008)~Acord Coupe (1999-)~Acord Type R~Civic Type R (2001-2006)(2007-,17"&lt;)~Civic Type S (2001-2006)(2007-,17"&lt;)~Integra Type R~Prelude (1997-2001)~Legend (1991-1999)</model> 
    

到X元素<model>

<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</model> 
    <model>Prelude (1997-2001)</model> 
    <model>Legend (1991-1999)</model> 

分離符號「 〜」

到目前爲止我沒有能夠做到這一點(沒有使用PHP,既不XSL轉換),因此尋求幫助;-(

  1. 如何組具有相同<stockode>物品進入1 <Item>根代替的X? (如我所說,如果車輪適用於很多車型,那麼它將在XML中多次使用不同的正義元素(有時也是<vehicle> - 但是在這個車輪的情況下也是如此)。僅適用於少數車型))?

因此,修改後的XML應該是:

<StockInfo> 
<Item> 
<stockcode>111111</stockcode> 
<vehicle>Ford</vehicle> 
<model>Escape (2000-)~Maverick (2001-)</model> 
<vehicle>Ford</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</model> 
<model>Prelude (1997-2001)</model> 
<model>Legend (1991-1999)</model> 
<wheel_size>18</wheel_size> 
<offset>35</offset> 
<offset>40</offset> 
<bolt_pattermn>5x114.3</bolt_pattermn> 
<brand>ANTERA</brand> 
<Velg_ID/> 
<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> 
<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> 
<qty_available>4.00000000</qty_available> 
<price>151110.00</price> 
<picture>41001 
</picture> 
</Item> 
</StockInfo> 
+0

你有什麼問題嗎? –

+0

編輯後更清晰我的問題! –

回答

0

這對我的作品(如解決我的第一問題):

<?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:template match="@*|node()"> 
<xsl:copy> 
<xsl:apply-templates select="@*|node()"/> 
</xsl:copy> 
</xsl:template> 
<xsl:template match="text()[contains(.,'~')]"> 
<xsl:variable name="elementName" select="name(..)"/> 
<xsl:call-template name="splitIntoElements"> 
<xsl:with-param name="baseName" select="name(..)" /> 
<xsl:with-param name="txt" select="." />  
</xsl:call-template> 
</xsl:template> 
<xsl:template name="splitIntoElements"> 
<xsl:param name="baseName" /> 
<xsl:param name="txt" /> 
<xsl:param name="delimiter" select="'~'" /> 
<xsl:variable name="first" select="substring-before($txt, $delimiter)" /> 
<xsl:variable name="remaining" select="substring-after($txt, $delimiter)" /> 
<xsl:element name="{$baseName}"> 
<xsl:choose> 
<xsl:when test="$first"> 
<xsl:value-of select="$first" /> 
</xsl:when> 
<xsl:otherwise> 
<xsl:value-of select="$txt" /> 
</xsl:otherwise> 
</xsl:choose> 
</xsl:element> 
<xsl:if test="$remaining"> 
<xsl:call-template name="splitIntoElements"> 
<xsl:with-param name="baseName" select="$baseName" /> 
<xsl:with-param name="txt" select="$remaining" /> 
<xsl:with-param name="delimiter" select="$delimiter" /> 
</xsl:call-template> 
</xsl:if> 
</xsl:template> 
<xsl:template match="model"> 
<xsl:apply-templates/> 
</xsl:template> 
</xsl:stylesheet> 
+0

它取自http://stackoverflow.com/questions/6657682/xslt-to-copy-over-all-nodes-and-split-on-delimiter並根據我的xml進行小修改! –