2012-11-13 2 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> 

당신은이 개 동일한 항목이 보시다시피 (같은 stockcode와; 항목은 자동차 바퀴입니다) 1 개 다른 항목을 ... 같은 항목은 X 시간이 될 수 있습니다 모든 다른 자동차 모델 (이 휠이 Audi A3, Audi A4, Citroen X에 적합하면 XML로 3 번, 다른 모든 요소는 제외하고 같은 값을 가짐)를 XML로 변환합니다.

내 질문 :

  1. 분리하는 방법/분할 요소 <model> : X 요소로

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

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

  2. 동일한 값을 가진 요소를 제거하는 방법 (내가 말했듯이 휠이 여러 자동차 모델에 적합하다면 여러 다른 요소를 사용하여 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에 따라 약간의 수정이 필요합니다! –