我是XML和XSL的新手。我正在尝试使用我们的供应商软件生成的XML字符串并将其转换为Excel格式。 XML可以具有重复的行,每行具有许多关联的子字段。子字段在行与行之间始终相同,并且顺序相同,但在不同的XML中可能不同。行数将随文件而变化。
我觉得这里有一个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 :(得分:0)
IIUC,您希望转置给定的表 - 行到列,字段到行。 silowing样式表显示了如何做到这一点。为简单起见,这会生成一个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>
应用于您的示例输入,(呈现)结果将为: