2016-05-20 226 views
0

我想將XML1轉換爲XML2.Here是我用來修改的XSLT。但沒有得到任何結果。有人能幫助我哪裏錯了嗎?通過XSLT將XML轉換爲XML

XML1 -

 <?xml version="1.0" encoding="ISO-8859-15"?> 
     <session> 
      <report LocalDateTime="2016-05-17T15:43:08"> 
      <title>Data Notes for Project Starter team</title> 
      <step1>Student</step1> 
      <step2>Value</step2> 
      <divider/> 
      <alldata>true</alldata> 
      <size>23</size> 
      <min>0.000000000000000</min> 
      <max>200.000000000000000</max> 
      <Data>  <labels1 labelSize="56" maxLabelLength="25">   
       <label>Mathew Bohrer</label> 
       <label>Susann Leftwich</label> 
       <label>Hanna Pack</label> 
       <label>Martin Gideon</label> 
       <label>Julienne Maki</label> 
       <label>Woodrow Barroso</label> 
       <label>Candance Cremin</label> 
       <label>Lael Mccoin</label> 
       </labels1> 
       <Set label="Closed">   
       <number>78.000000000000000</number> 
       <link>studentproject.dll?testc34$amp;</link> 
       <number>24.000000000000000</number> 
       <link>studentproject.dll?testt56$amp;</link> 
       <number>32.000000000000000</number> 
        <link>studentproject.dll?testh67$amp;</link> 
       <number>75.000000000000000</number> 
        <link>studentproject.dll?testj77$amp;</link> 
       <number>11.000000000000000</number> 
        <link>studentproject.dll?testk234$amp;</link> 
       <number>47.000000000000000</number> 
        <link>studentproject.dll?testx239$amp;</link> 
       <number>24.000000000000000</number> 
        <link>studentproject.dll?testb567$amp;</link> 
       <number>10.000000000000000</number> 
       </Set> 
       <Lines/> 
      </Data> 
      </report> 
      <faculty type="12"> <lastCode id="19"/> 
      </faculty> 
     </session> 

XML2-

<?xml version="1.0" encoding="ISO-8859-15"?> 
    <session> 
     <report LocalDateTime="2016-05-17T15:43:08"> 
     <title>Data Notes for Project Starter team</title> 
     <step1>Student</step1> 
     <step2>Value</step2> 
     <divider/> 
     <alldata>true</alldata> 
     <size>23</size> 
     <min>0.000000000000000</min> 
     <max>200.000000000000000</max> 
     <Data>  

     <DataSet label="Closed" > 
     <label>Mathew Bohrer</label> 
     <number>78.000000000000000</number> 
     </DataSet> 

     <DataSet label="Closed" > 
     <label>Susann Leftwich</label> 
     <number>24.000000000000000</number> 
     </DataSet> 
     <DataSet label="Closed" > 
     <label>Hanna Pack</label> 
     <number>32.000000000000000</number> 
     </DataSet> 
     <DataSet label="Closed" > 
     <label>Martin Gideon</label> 
     <number>75.000000000000000</number> 
     </DataSet> 
     <DataSet label="Closed" > 
     <label>Julienne Maki</label> 
     <number>11.000000000000000</number> 
     </DataSet> 
     <DataSet label="Closed" > 
      <label>Woodrow Barroso</label> 
     <number>47.000000000000000</number> 
     </DataSet> 

     <DataSet label="Closed" > 
     <label>Candance Cremin</label> 
     <number>24.000000000000000</number> 
     </DataSet> 
     <DataSet label="Closed" > 
     <label>Lael Mccoin</label> 
     <number>10.000000000000000</number> 
     </DataSet> 
      <Lines/> 
     </Data> 
     </report> 
     <faculty type="12"> <lastCode id="19"/> 
     </faculty> 
    </session> 

這裏是我的修整XSLT。但沒有得到任何結果。有人能幫助我哪裏錯了嗎?

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

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

    <xsl:template match="link"/> 

    <xsl:template match="/session/report/Data"> 
    <xsl:variable name="pos"> 
     <xsl:number/> 
    </xsl:variable> 
    <xsl:copy> 
     <xsl:apply-templates select="/label[1]"/> 

    </xsl:copy>  
    </xsl:template> 

    <xsl:template match="value"> 

    <xsl:element name="DataSet"> 
    <label><xsl:value-of select="."/> </label> 
    <number><xsl:value-of select="."/> </number> 
    </xsl:element> 
    </xsl:template> 


</xsl:stylesheet> 

回答

1

你必須使用,以獲得相應的編號,名稱的位置,如果我的理解是正確的:

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

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

    <xsl:template match="link | Set"/> 

    <xsl:template match="/session/report/Data"> 
     <xsl:copy> 
      <xsl:apply-templates select="labels1/label"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="label"> 
     <xsl:variable name="pos" select="position()"/> 
     <DataSet label="{../../Set/@label}"> 
      <label> 
       <xsl:value-of select="."/> 
      </label> 
      <number> 
       <xsl:value-of select="../../Set/number[$pos]"/> 
      </number> 
     </DataSet> 
    </xsl:template> 

</xsl:stylesheet>