通过XSLT将XML转换为XML

时间:2016-05-20 21:42:26

标签: xml xslt

我想将XML1转换为XML2.Here是我修改过的XSLT。但没有得到任何结果。有人可以帮助我在哪里出错吗?

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 个答案:

答案 0 :(得分: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>