2013-07-30 31 views
1

我有一個相當不好格式化的輸入xml,需要使用xslt 1.0重新組織。 必須更改層次結構,以便細節線分成上下兩部分,每個部分都包含左側和右側部分。直到現在我還沒有創造出所需的輸出。 有人可以幫助我,提前謝謝。創建新的結構和移動元素與xslt

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<document> 
<Header_Label_Header_1> 
    <Label_Header_Header_Company>1001</Label_Header_Header_Company> 
    <Label_Header_Header_Unique_Sequence_Number>1</Label_Header_Header_Unique_Sequence_Number> 
    <Detail_Line> 
     <Line_Print_Description_1>PAR Wdfl Vivida bianco strukt.</Line_Print_Description_1> 
     <Line_Print_Description_2>30x60cm</Line_Print_Description_2> 
    </Detail_Line> 
    <Detail_Line> 
     <Line_Print_Description_1>PAR Wdfl Secret bianco 1.</Line_Print_Description_1> 
     <Line_Print_Description_2>29,5x59,5cm</Line_Print_Description_2> 
    </Detail_Line> 
</Header_Label_Header_1> 
<Header_Label_Header_2> 
    <Label_Header_Header_Company>00500</Label_Header_Header_Company> 
    <Label_Header_Header_Unique_Sequence_Number>2</Label_Header_Header_Unique_Sequence_Number> 
    <Detail_Line> 
     <Line_Print_Description_1>IRS Fstzg Porcelainw.Brownwood</Line_Print_Description_1> 
     <Line_Print_Description_2>frb. R9, 15x120cm</Line_Print_Description_2> 
    </Detail_Line> 
    <Detail_Line> 
     <Line_Print_Description_1>IRS Fstzg Porcelainw.Greywood</Line_Print_Description_1> 
     <Line_Print_Description_2>frb. R9, 15x120cm</Line_Print_Description_2> 
    </Detail_Line> 
    <Detail_Line> 
     <Line_Print_Description_1>IRS Fstzg Porcelainw.Blackwood</Line_Print_Description_1> 
     <Line_Print_Description_2>frb. R9, 15x120cm</Line_Print_Description_2> 
    </Detail_Line> 
</Header_Label_Header_2> 
<Header_Label_Header_3> 
    <Label_Header_Header_Company>00500</Label_Header_Header_Company> 
    <Label_Header_Header_Unique_Sequence_Number>3</Label_Header_Header_Unique_Sequence_Number> 
    <Detail_Line> 
     <Line_Print_Description_1>RAK Fstzg Ardesia black gl.str</Line_Print_Description_1> 
     <Line_Print_Description_2>frb, R9, 30x60cm</Line_Print_Description_2> 
    </Detail_Line> 
</Header_Label_Header_3> 
<Header_Label_Header_4> 
    <Label_Header_Header_Company>00500</Label_Header_Header_Company> 
    <Label_Header_Header_Unique_Sequence_Number>4</Label_Header_Header_Unique_Sequence_Number> 
    <Detail_Line> 
     <Line_Print_Description_1>BAUMIT KlebeSpachtel 25kg</Line_Print_Description_1> 
     <Line_Print_Description_2></Line_Print_Description_2> 
    </Detail_Line> 
</Header_Label_Header_4> 
<Header_Label_Header_1> 
    <Label_Header_Header_Company>1001</Label_Header_Header_Company> 
    <Label_Header_Header_Unique_Sequence_Number>1</Label_Header_Header_Unique_Sequence_Number> 
    <Detail_Line> 
     <Line_Print_Description_1>PAR Wdfl Vivida bianco strukt.</Line_Print_Description_1> 
     <Line_Print_Description_2>30x60cm</Line_Print_Description_2> 
    </Detail_Line> 
    <Detail_Line> 
     <Line_Print_Description_1>PAR Wdfl Secret bianco 1.</Line_Print_Description_1> 
     <Line_Print_Description_2>29,5x59,5cm</Line_Print_Description_2> 
    </Detail_Line> 
</Header_Label_Header_1> 
<Header_Label_Header_2> 
    <Label_Header_Header_Company>00500</Label_Header_Header_Company> 
    <Label_Header_Header_Unique_Sequence_Number>2</Label_Header_Header_Unique_Sequence_Number> 
    <Detail_Line> 
     <Line_Print_Description_1>IRS Fstzg Porcelainw.Brownwood</Line_Print_Description_1> 
     <Line_Print_Description_2>frb. R9, 15x120cm</Line_Print_Description_2> 
    </Detail_Line> 
    <Detail_Line> 
     <Line_Print_Description_1>IRS Fstzg Porcelainw.Greywood</Line_Print_Description_1> 
     <Line_Print_Description_2>frb. R9, 15x120cm</Line_Print_Description_2> 
    </Detail_Line> 
    <Detail_Line> 
     <Line_Print_Description_1>IRS Fstzg Porcelainw.Blackwood</Line_Print_Description_1> 
     <Line_Print_Description_2>frb. R9, 15x120cm</Line_Print_Description_2> 
    </Detail_Line> 
</Header_Label_Header_2> 
<Header_Label_Header_3> 
    <Label_Header_Header_Company>00500</Label_Header_Header_Company> 
    <Label_Header_Header_Unique_Sequence_Number>3</Label_Header_Header_Unique_Sequence_Number> 
    <Detail_Line> 
     <Line_Print_Description_1>RAK Fstzg Ardesia black gl.str</Line_Print_Description_1> 
     <Line_Print_Description_2>frb, R9, 30x60cm</Line_Print_Description_2> 
    </Detail_Line> 
</Header_Label_Header_3> 
<Header_Label_Header_4> 
    <Label_Header_Header_Company>00500</Label_Header_Header_Company> 
    <Label_Header_Header_Unique_Sequence_Number>4</Label_Header_Header_Unique_Sequence_Number> 
    <Detail_Line> 
     <Line_Print_Description_1>BAUMIT KlebeSpachtel 25kg</Line_Print_Description_1> 
     <Line_Print_Description_2></Line_Print_Description_2> 
    </Detail_Line> 
</Header_Label_Header_4> 
</document> 

輸出必須是這樣的:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<document> 
<Header_Label_Upper> 
    <Label_Left> 
     <Label_Header_Header_Company>1001</Label_Header_Header_Company> 
     <Label_Header_Header_Unique_Sequence_Number>1</Label_Header_Header_Unique_Sequence_Number> 
    </Label_Left> 
      <Detail_Line_Left> 
       <Line_Print_Description_1>PAR Wdfl Vivida bianco strukt.</Line_Print_Description_1> 
       <Line_Print_Description_2>30x60cm</Line_Print_Description_2> 
      </Detail_Line_Left> 
      <Detail_Line_Left> 
       <Line_Print_Description_1>PAR Wdfl Secret bianco 1.</Line_Print_Description_1> 
       <Line_Print_Description_2>29,5x59,5cm</Line_Print_Description_2> 
    </Detail_Line_Left> 
    <Label_Right> 
     <Label_Header_Header_Company>00500</Label_Header_Header_Company> 
     <Label_Header_Header_Unique_Sequence_Number>2</Label_Header_Header_Unique_Sequence_Number> 
    </Label_Right> 
      <Detail_Line_Right> 
       <Line_Print_Description_1>IRS Fstzg Porcelainw.Brownwood</Line_Print_Description_1> 
       <Line_Print_Description_2>frb. R9, 15x120cm</Line_Print_Description_2> 
      </Detail_Line_Right> 
      <Detail_Line_Right> 
       <Line_Print_Description_1>IRS Fstzg Porcelainw.Greywood</Line_Print_Description_1> 
       <Line_Print_Description_2>frb. R9, 15x120cm</Line_Print_Description_2> 
      </Detail_Line_Right> 
      <Detail_Line_Right> 
       <Line_Print_Description_1>IRS Fstzg Porcelainw.Blackwood</Line_Print_Description_1> 
       <Line_Print_Description_2>frb. R9, 15x120cm</Line_Print_Description_2> 
      </Detail_Line_Right> 
</Header_Label_Upper> 
<Header_Label_Lower> 
    <Label_Left> 
     <Label_Header_Header_Company>00500</Label_Header_Header_Company> 
     <Label_Header_Header_Unique_Sequence_Number>3</Label_Header_Header_Unique_Sequence_Number> 
    </Label_Left> 
      <Detail_Line_Left> 
       <Line_Print_Description_1>RAK Fstzg Ardesia black gl.str</Line_Print_Description_1> 
       <Line_Print_Description_2>frb, R9, 30x60cm</Line_Print_Description_2> 
      </Detail_Line_Left> 
    <Label_Right> 
     <Label_Header_Header_Company>00500</Label_Header_Header_Company> 
     <Label_Header_Header_Unique_Sequence_Number>4</Label_Header_Header_Unique_Sequence_Number> 
    </Label_Right> 
      <Detail_Line_Right> 
       <Line_Print_Description_1>BAUMIT KlebeSpachtel 25kg</Line_Print_Description_1> 
       <Line_Print_Description_2></Line_Print_Description_2> 
      </Detail_Line_Right> 
</Header_Label_Lower> 
<Header_Label_Upper> 
    <Label_Left> 
     <Label_Header_Header_Company>1001</Label_Header_Header_Company> 
     <Label_Header_Header_Unique_Sequence_Number>1</Label_Header_Header_Unique_Sequence_Number> 
    </Label_Left> 
      <Detail_Line_Left> 
       <Line_Print_Description_1>PAR Wdfl Vivida bianco strukt.</Line_Print_Description_1> 
       <Line_Print_Description_2>30x60cm</Line_Print_Description_2> 
      </Detail_Line_Left> 
      <Detail_Line_Left> 
       <Line_Print_Description_1>PAR Wdfl Secret bianco 1.</Line_Print_Description_1> 
       <Line_Print_Description_2>29,5x59,5cm</Line_Print_Description_2> 
    </Detail_Line_Left> 
    <Label_Right> 
     <Label_Header_Header_Company>00500</Label_Header_Header_Company> 
     <Label_Header_Header_Unique_Sequence_Number>2</Label_Header_Header_Unique_Sequence_Number> 
    </Label_Right> 
      <Detail_Line_Right> 
       <Line_Print_Description_1>IRS Fstzg Porcelainw.Brownwood</Line_Print_Description_1> 
       <Line_Print_Description_2>frb. R9, 15x120cm</Line_Print_Description_2> 
      </Detail_Line_Right> 
      <Detail_Line_Right> 
       <Line_Print_Description_1>IRS Fstzg Porcelainw.Greywood</Line_Print_Description_1> 
       <Line_Print_Description_2>frb. R9, 15x120cm</Line_Print_Description_2> 
      </Detail_Line_Right> 
      <Detail_Line_Right> 
       <Line_Print_Description_1>IRS Fstzg Porcelainw.Blackwood</Line_Print_Description_1> 
       <Line_Print_Description_2>frb. R9, 15x120cm</Line_Print_Description_2> 
      </Detail_Line_Right> 
</Header_Label_Upper> 
<Header_Label_Lower> 
    <Label_Left> 
     <Label_Header_Header_Company>00500</Label_Header_Header_Company> 
     <Label_Header_Header_Unique_Sequence_Number>3</Label_Header_Header_Unique_Sequence_Number> 
    </Label_Left> 
      <Detail_Line> 
       <Line_Print_Description_1>RAK Fstzg Ardesia black gl.str</Line_Print_Description_1> 
       <Line_Print_Description_2>frb, R9, 30x60cm</Line_Print_Description_2> 
      </Detail_Line> 
    <Label_Right> 
     <Label_Header_Header_Company>00500</Label_Header_Header_Company> 
     <Label_Header_Header_Unique_Sequence_Number>4</Label_Header_Header_Unique_Sequence_Number> 
    </Label_Right> 
      <Detail_Line> 
       <Line_Print_Description_1>BAUMIT KlebeSpachtel 25kg</Line_Print_Description_1> 
       <Line_Print_Description_2></Line_Print_Description_2> 
      </Detail_Line> 
</Header_Label_Lower> 
</document> 

回答

1

鑑於你的示例XML,下面的XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
     <xsl:output method="xml" version="1.0" indent="yes" standalone="no"/> 
     <xsl:strip-space elements="*"/> 

     <!-- The identity transform. --> 
     <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
     </xsl:template> 

     <!-- Match the root node. --> 
     <xsl:template match="document"> 
     <xsl:copy> 
      <!-- Restructure each subset of four Header_Label_Header elements. --> 
      <xsl:for-each select="*[position() mod 4 = 1]"> 
      <xsl:call-template name="subset"> 
       <xsl:with-param name="subset-nodes" select=". | following-sibling::*[position() &lt; 4]"/> 
      </xsl:call-template> 
      </xsl:for-each> 
     </xsl:copy> 
     </xsl:template> 

     <xsl:template name="subset"> 
     <xsl:param name="subset-nodes"/> 
     <!-- Select the first and third elements and give them appropriate upper/lower labels. --> 
     <xsl:for-each select="$subset-nodes[position() mod 2 = 1]"> 
      <xsl:choose> 
      <xsl:when test="position() = 1"> 
       <Header_Label_Upper> 
       <!-- Select this element and the one that follows it. --> 
       <xsl:apply-templates select=". | following-sibling::*[position() &lt; 2]"/> 
       </Header_Label_Upper> 
      </xsl:when> 
      <xsl:otherwise> 
       <Header_Label_Lower> 
       <!-- Select this element and the one that follows it. --> 
       <xsl:apply-templates select=". | following-sibling::*[position() &lt; 2]"/> 
       </Header_Label_Lower> 
      </xsl:otherwise> 
      </xsl:choose> 
     </xsl:for-each> 
     </xsl:template> 

    <!-- Match the Header_Label_Header elements and give them appropriate left/right labels. --> 
    <xsl:template match="*[contains(local-name(), 'Header_Label_Header_')]"> 
    <xsl:choose> 
     <xsl:when test="position() = 1"> 
     <Label_Left> 
      <xsl:apply-templates select="*[contains(local-name(), 'Label_Header_Header_')]"/> 
     </Label_Left> 
     <xsl:apply-templates select="Detail_Line" mode="Left"/> 
     </xsl:when> 
     <xsl:otherwise> 
     <Label_Right> 
      <xsl:apply-templates select="*[contains(local-name(), 'Label_Header_Header_')]"/> 
     </Label_Right> 
     <xsl:apply-templates select="Detail_Line" mode="Right"/> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 

    <xsl:template match="Detail_Line" mode="Left"> 
    <Detail_Line_Left> 
     <xsl:apply-templates/> 
    </Detail_Line_Left> 
    </xsl:template> 

    <xsl:template match="Detail_Line" mode="Right"> 
    <Detail_Line_Right> 
     <xsl:apply-templates/> 
    </Detail_Line_Right> 
    </xsl:template> 
</xsl:stylesheet> 

輸出所需的XML。

該模板可能可以改進,因爲有一些重複的代碼,但它的工作。

+0

感謝本幫忙! –

+0

不客氣,@ludoaerts。如果這解決了你的問題,你會考慮[接受它作爲答案?] –

+0

嗨,本,再次感謝解決方案。但是我對其他文件做了一些測試,如果有更多4個部分存在問題。 –