更改XML的布局-将XML元素值拆分为多个元素;删除不必要的元素

更改XML的布局-将XML元素值拆分为多个元素;删除不必要的元素,xml,xslt,Xml,Xslt,我有这样的XML: <StockInfo> <Item> <stockcode>111111</stockcode> <vehicle>Ford</vehicle> <model>Escape (2000-)~Maverick (2001-)</model> <width>8</width> <wheel_size>18</wheel_size> <

我有这样的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>

111111
河流浅水处
逃脱(2000-)~Maverick(2001-)
8.
18
35
5x114.3
安泰拉
DK-135259671 x1
3
1110
410110
111111
本田
思域(5skr,2001-2006)(2006-~Accord(2003-2008)~Acord Coupe(1999-~Acord Type R)~Civic Type R(2001-2006)(2007-,17”)~Civic Type S(2001-2006)(2007-,17”)~Integra Type R~前奏曲(1997-2001)~Legend(1991-1999)
8.
18
40
5x114.3
安泰拉
DK-135259641 x1
3
1110
410110
2222222
宝马
6(e63/642004-2011)~M6(e63/642004-2011)
9
18
15
5x120
AEZ
阿雷斯
DK-ZJB3-x1
4
151110
41001
如您所见,有两个相同的项目(具有相同的库存代码;项目是车轮)和一个不同的项目。。。对于每种不同的车型,同一项可以X次转换为XML(如果此车轮适用于奥迪A3、奥迪A4、雪铁龙X,则它将3次转换为XML,所有其他元素(除了和具有相同的值))

我的问题是:

  • 如何分离/拆分元素

    Civic(5skr,2001-2006)(2006-~Accord(2003-2008)~Acord Coupe(1999-~Acord Type R)~Civic Type R(2001-2006)(2007-,17”)~Civic Type S(2001-2006)(2007-,17”)~Integra Type R~序曲(1997-2001)~Legend(1991-1999)
    
  • 进入X元素

    Civic(5skr,2001-2006)(2006-)
    协议(2003-2008)
    Acord Coupe(1999-)
    Acord类型R
    城市类型R(2001-2006)(2007-17)
    城市类型S(2001-2006)(2007-17)
    积分型R
    序曲(1997-2001)
    传奇(1991-1999)
    
    分隔符号为“~”

    到目前为止,我还不能做到这一点(既不使用php,也不使用xsl转换),因此寻求帮助-(

  • 如何将具有相同
    的项目分组为1
    根目录而不是X

  • 如何删除具有相同值的元素(如我所说,如果wheel适用于许多车型,那么它将在XML中多次使用不同的just元素(有时也使用
    -但仅在该wheel仅适用于少数车型的情况下使用))

  • 因此,修改后的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>
    
    
    111111
    河流浅水处
    逃脱(2000-)~Maverick(2001-)
    河流浅水处
    思域(5skr,2001-2006)(2006-)
    协议(2003-2008)
    Acord Coupe(1999-)
    Acord类型R
    城市类型R(2001-2006)(2007-17)
    城市类型S(2001-2006)(2007-17)
    积分型R
    序曲(1997-2001)
    传奇(1991-1999)
    18
    35
    40
    5x114.3
    安泰拉
    3
    1110
    410110
    2222222
    宝马
    6(e63/642004-2011)
    M6(e63/642004-2011)
    M6(e63/642004-2011)
    9
    18
    15
    5x120
    AEZ
    阿雷斯
    4
    151110
    41001
    
    这对我很有用(作为我第一个问题的解决方案):


    编辑了这篇文章,以便更清楚地回答我的问题!这篇文章取自我的xml,并根据我的xml进行了一些小的修改!
        <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>
    
    <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>
    
    <?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>