2016-06-13 61 views
0

我是XML和XSL的新手。我正在嘗試使用供應商軟件生成的XML字符串並將其轉換爲Excel格式。 XML可以有重複的行,每行都有許多關聯的子字段。從Row到Row的子字段總是相同的,並且順序相同,但在不同的XML中可能會有所不同。行數將從文件更改爲文件。XSL將父項顯示爲列,並將匹配的子項顯示爲行

我有一種感覺,這裏有一個for-each,可能是嵌套的,或者使用關係位置,但是我在查詢函數時遇到了麻煩。我已經回顧了很多樣本​​,但是每個解決方案都是針對這個問題量身定製的,並且我沒有找到一個需要的輸出與我的樣本相同的地方。

任何幫助,或至少在正確的方向微調,將不勝感激。

謝謝!

示例XML:

<?xml version="1.0" standalone="yes" ?> 
<RpcData SrcNm="SnapshotBuckets" SrcTyp="DIR" ClientID="000" LoanNo="0000000000" Borrower="" RsltCd="0"> 
    <RepeatingFieldSet Nm="Hazard" Type="All Data" Count="3"> 
    <Row Index="1"> 
     <Fld Nm="Type">A</Fld> 
     <Fld Nm="AgentCode">TESTAP</Fld> 
     <Fld Nm="Agent City">ANYTOWN</Fld> 
     <Fld Nm="Agent Desc Line 1">APPLE</Fld> 
     <Fld Nm="Agent Desc Line 2">PICKERS</Fld> 
     <Fld Nm="Agent Desc Line 3">123 MAIN ST</Fld> 
     <Fld Nm="Agent Phone">(718) 555-1212</Fld> 
     <Fld Nm="Agent State">AL</Fld> 
     <Fld Nm="Agent ZIP Code">00001</Fld> 
    </Row> 
    <Row Index="2"> 
     <Fld Nm="Type">B</Fld> 
     <Fld Nm="AgentCode">TESTBA</Fld> 
     <Fld Nm="Agent City">ANYTOWN</Fld> 
     <Fld Nm="Agent Desc Line 1">BANANA</Fld> 
     <Fld Nm="Agent Desc Line 2">BUNCHERS</Fld> 
     <Fld Nm="Agent Desc Line 3">456 MAIN ST</Fld> 
     <Fld Nm="Agent Phone">(718) 555-1213</Fld> 
     <Fld Nm="Agent State">AK</Fld> 
     <Fld Nm="Agent ZIP Code">00002</Fld> 
    </Row> 
    <Row Index="3"> 
     <Fld Nm="Type">C</Fld> 
     <Fld Nm="AgentCode">TESTCH</Fld> 
     <Fld Nm="Agent City">ANYTOWN</Fld> 
     <Fld Nm="Agent Desc Line 1">CHERRY</Fld> 
     <Fld Nm="Agent Desc Line 2">PITTERS</Fld> 
     <Fld Nm="Agent Desc Line 3">789 MAIN ST</Fld> 
     <Fld Nm="Agent Phone">(718) 555-1214</Fld> 
     <Fld Nm="Agent State">CA</Fld> 
     <Fld Nm="Agent ZIP Code">00003</Fld> 
    </Row> 
    </RepeatingFieldSet> 
</RpcData> 

所需的輸出:

 
Field     1    2    3 
Type     A    B    C 
AgentCode    TESTAP   TESTBA   TESTCH 
Agent City    ANYTOWN   ANYTOWN   ANYTOWN 
Agent Desc Line 1  APPLE   BANANA   CHERRY 
Agent Desc Line 2  PICKERS   BUNCHERS  PITTERS 
Agent Desc Line 3  123 MAIN ST  456 MAIN ST  789 MAIN ST 
Agent Phone    (718) 555-1212 (718) 555-1213 (718) 555-1214 
Agent State    AL    AK    CA 
Agent ZIP Code   00001   00002   00003 

XSL至今:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns="urn:schemas-microsoft-com:office:spreadsheet" 
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
xmlns:x="urn:schemas-microsoft-com:office:excel"> 
    <xsl:template match="/"> 
     <xsl:processing-instruction name="mso-application"><xsl:text>progid="Excel.Sheet"</xsl:text></xsl:processing-instruction> 
     <Workbook> 
      <Worksheet ss:Name="Sheet1"> 
       <Table> 
        <Row> 
         <Cell>Business Name</Cell> 
         <xsl:for-each select="RpcData/RepeatingFieldSet/Row"> 
          <Cell> 
           <xsl:value-of select="@Index"/> 
          </Cell> 
         </xsl:for-each> 
        </Row> 
        <xsl:for-each select="RpcData/RepeatingFieldSet/*/Fld"> 
         <Row> 
          <Cell> 
           <xsl:value-of select="@Nm"/> 
          </Cell> 
          <xsl:for-each select="*"> 
           <Cell> 
            <xsl:value-of select="."/> 
           </Cell> 
          </xsl:for-each> 
         </Row> 
        </xsl:for-each> 
       </Table> 
      </Worksheet> 
     </Workbook> 
    </xsl:template>  
</xsl:stylesheet> 

回答

0

IIUC,要給定的表 - 行列,字段爲行。流露的樣式表顯示瞭如何做到這一點。爲了簡單起見,這會生成一個HTML表格 - 對Excel工作表的調整應該是微不足道的。

XSLT 1.0

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

<xsl:template match="/RpcData"> 
    <xsl:variable name="col" select="RepeatingFieldSet/Row"/> 
    <xsl:variable name="row" select="RepeatingFieldSet/Row[1]/Fld"/> 
    <table border="1"> 
     <!-- header row --> 
     <tr> 
      <th>Field</th> 
      <xsl:for-each select="$col"> 
       <th> 
        <xsl:value-of select="@Index"/> 
       </th> 
      </xsl:for-each> 
     </tr> 
     <!-- data rows --> 
     <xsl:for-each select="$row"> 
      <xsl:variable name="i" select="position()"/> 
      <tr> 
       <th><xsl:value-of select="@Nm"/></th> 
       <xsl:for-each select="$col"> 
        <td> 
         <xsl:value-of select="Fld[$i]"/> 
        </td> 
       </xsl:for-each> 
      </tr>  
     </xsl:for-each> 
    </table> 
</xsl:template> 

</xsl:stylesheet> 

適用於你的榜樣輸入時,(渲染)的結果將是:

enter image description here

+0

正是我想要的。非常感謝! – ImagineMBE