如何使用 XSLT 将 XML 转换为表 [代码更新 11/6] - IT屋-程序员软件开发技术分享社区
首页
其他开发
如何使用 XSLT 将 XML 转换为表 [代码更新 11/6]
如何使用 XSLT 将 XML 转换为表 [代码更新 11/6]
[英] How to use XSLT to convert a XML to Table [CODE UPDATED 11/6]
本文介绍了如何使用 XSLT 将 XML 转换为表 [代码更新 11/6]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 XML 文件想要通过 XSLT 转换为表格或 CSV.我尝试通过 Altova MapForce 2014 来映射列,但仍然无法生成结果.非常感谢.
我有 XML 文件
<Person id="756252" date="15-Oct-2014"><性别>男性</性别><姓名详情><Name NameType="主名称"><名称值><FirstName>Ken</FirstName><姓氏>吴</姓氏></NameValue></姓名><Name NameType="AKA"><名称值><FirstName>Kenneth</FirstName><姓氏>吴</姓氏></NameValue></姓名><Name NameType="AKA2"><名称值><FirstName>CAN</FirstName><姓氏>吴</姓氏></NameValue></姓名></姓名详情><说明><Description Description1="11" Description2="12" Description3="13"/><Description Description1="21" Description2="22" Description3="23"/><Description Description1="31" Description2="32" Description3="33"/></描述><日期详情><Date DateType="出生日期"><DateValue Year="1990"/><DateValue Year="1991"/></日期><Date DateType="发行日期"><DateValue Year="2000"/><DateValue Year="2001"/></日期></日期详情></人><Person id="253555" date="14-Oct-2014"><性别>男性</性别><姓名详情><Name NameType="主名称"><名称值><名字>彼得</名字><姓氏>赖</姓氏></NameValue></姓名></姓名详情><说明><Description Description1="11" Description2="12" Description3="13"/><Description Description1="21" Description2="22"/></描述><Date DateType="出生日期"><DateValue Year="1992"/></日期></人></记录>
预期结果
|ID |Date |Gender |NameType |FirstName |SurName |Description1 |Description2 |Description3 |DateType |Year||756252 |15-Oct-2014 |男性 |主要姓名 |Ken |Wu |11 |12 |13 |出生日期 |1990||756252 |2014 年 10 月 15 日 |男性 |主要姓名 |Ken |Wu |21 |22 |23 |出生日期 |1990||756252 |2014 年 10 月 15 日 |男性 |主要姓名 |Ken |Wu |31 |32 |33 |出生日期 |1990||756252 |15-Oct-2014 |男性 |AKA |Kenneth |Wu |11 |12 |13 |出生日期 |1990||756252 |15-Oct-2014 |男性 |AKA |Kenneth |Wu |21 |22 |23 |出生日期 |1990||756252 |15-Oct-2014 |男性 |AKA |Kenneth |Wu |31 |32 |33 |出生日期 |1990||756252 |15-Oct-2014 |男性 |AKA2 |CAN |Wu |11 |12 |13 |出生日期 |1990||756252 |15-Oct-2014 |男性 |AKA2 |CAN |Wu |21 |22 |23 |出生日期 |1990||756252 |2014 年 10 月 15 日 |男性 |AKA2 |CAN |吴 |31 |32 |33 |出生日期 |1990||756252 |2014 年 10 月 15 日 |男性 |主要姓名 |Ken |Wu |11 |12 |13 |发行日期 |1991||756252 |2014 年 10 月 15 日 |男性 |主要姓名 |Ken |Wu |21 |22 |23 |发行日期 |1991||756252 |2014 年 10 月 15 日 |男性 |主要姓名 |Ken |Wu |31 |32 |33 |发行日期 |1991||756252 |15-Oct-2014 |男性 |AKA |Kenneth |Wu |11 |12 |13 |发行日期 |1991||756252 |15-Oct-2014 |男性 |AKA |Kenneth |Wu |21 |22 |23 |发行日期 |1991||756252 |15-Oct-2014 |男性 |AKA |Kenneth |Wu |31 |32 |33 |发行日期 |1991||756252 |15-Oct-2014 |男性 |AKA2 |CAN |Wu |11 |12 |13 |发行日期 |1991||756252 |2014 年 10 月 15 日 |男性 |AKA2 |CAN |吴 |21 |22 |23 |发行日期 |1991||756252 |2014 年 10 月 15 日 |男性 |AKA2 |CAN |吴 |31 |32 |33 |发行日期 |1991||756252 |2014 年 10 月 15 日 |男性 |主要姓名 |Ken |Wu |11 |12 |13 |发行日期 |2000||756252 |2014 年 10 月 15 日 |男性 |主要姓名 |Ken |Wu |21 |22 |23 |发行日期 |2000||756252 |2014 年 10 月 15 日 |男性 |主要姓名 |Ken |Wu |31 |32 |33 |发行日期 |2000||756252 |15-Oct-2014 |男性 |AKA |Kenneth |Wu |11 |12 |13 |发行日期 |2000||756252 |15-Oct-2014 |男性 |AKA |Kenneth |Wu |21 |22 |23 |发行日期 |2000||756252 |15-Oct-2014 |男性 |AKA |Kenneth |Wu |31 |32 |33 |发行日期 |2000||756252 |15-Oct-2014 |男 |AKA2 |CAN |Wu |11 |12 |13 |发行日期 |2000||756252 |15-Oct-2014 |男 |AKA2 |CAN |Wu |21 |22 |23 |发行日期 |2000||756252 |15-Oct-2014 |男 |AKA2 |CAN |Wu |31 |32 |33 |发行日期 |2000||756252 |2014 年 10 月 15 日 |男性 |主要姓名 |Ken |Wu |11 |12 |13 |发行日期 |2001||756252 |2014 年 10 月 15 日 |男性 |主要姓名 |Ken |Wu |21 |22 |23 |发行日期 |2001||756252 |2014 年 10 月 15 日 |男性 |主要姓名 |Ken |Wu |31 |32 |33 |发行日期 |2001||756252 |15-Oct-2014 |男性 |AKA |Kenneth |Wu |11 |12 |13 |发行日期 |2001||756252 |15-Oct-2014 |男性 |AKA |Kenneth |Wu |21 |22 |23 |发行日期 |2001||756252 |15-Oct-2014 |男性 |AKA |Kenneth |Wu |31 |32 |33 |发行日期 |2001||756252 |15-Oct-2014 |男 |AKA2 |CAN |Wu |11 |12 |13 |发行日期 |2001||756252 |15-Oct-2014 |男 |AKA2 |CAN |Wu |21 |22 |23 |发行日期 |2001||756252 |15-Oct-2014 |男 |AKA2 |CAN |Wu |31 |32 |33 |发行日期 |2001||253555 |2014 年 10 月 14 日 |男性 |主要姓名 |彼得 |赖 |11 |12 |13 |发行日期 |1992||253555 |2014 年 10 月 14 日 |男性 |主要姓名 |Peter |Lai |21 |22 ||发行日期 |1992|
我为测试而创建的 XSL
<头><title>记录</title>头部><身体><表格边框=1"><tr><th>ID</th><第>日期</th><th>性别</th><th>NameType</th><th>FirstName</th><th>SurName</th>第 1 个描述第 2 个描述第 3 个描述</tr><xsl:for-each select="Records/Person/NameDetails/Name"><tr><td><xsl:value-of select="../../@id"/></td><td><xsl:value-of select="../../@date"/></td><td><xsl:value-of select="../../Gender"/></td><td><xsl:value-of select="@NameType"/></td><td><xsl:value-of select="NameValue/FirstName"/></td><td><xsl:value-of select="NameValue/Surname"/></td><td><xsl:value-of select="../../Descriptions/Description/@Description1"/></td><td><xsl:value-of select="../../Descriptions/Description/@Description2"/></td><td><xsl:value-of select="../../Descriptions/Description/@Description3"/></td></tr></xsl:for-each>
</html></xsl:模板></xsl:stylesheet>
解决方案
这里需要两个嵌套循环:
<预><代码>...<xsl:for-each select="Records/Person/NameDetails/Name"><xsl:variable name="NameType" select="@NameType"/><xsl:variable name="FirstName" select="NameValue/FirstName"/><xsl:variable name="Surname" select="NameValue/Surname"/><xsl:for-each select="../../Descriptions/Description"><tr><td><xsl:value-of select="../../@id"/></td><td><xsl:value-of select="../../@date"/></td><td><xsl:value-of select="../../Gender"/></td><td><xsl:value-of select="$NameType"/></td><td><xsl:value-of select="$FirstName"/></td><td><xsl:value-of select="$Surname"/></td><td><xsl:value-of select="@Description1"/></td><td><xsl:value-of select="@Description2"/></td><td><xsl:value-of select="@Description3"/></td></tr></xsl:for-each></xsl:for-each>...
<小时>
要避免将所有名称详细信息存储在单个变量中,您可以执行以下操作:
<xsl:variable name="name" select="."/><xsl:for-each select="../../Descriptions/Description"><tr><td><xsl:value-of select="../../@id"/></td><td><xsl:value-of select="../../@date"/></td><td><xsl:value-of select="../../Gender"/></td><td><xsl:value-of select="$name/@NameType"/></td><td><xsl:value-of select="$name/NameValue/FirstName"/></td><td><xsl:value-of select="$name/NameValue/Surname"/></td><td><xsl:value-of select="@Description1"/></td><td><xsl:value-of select="@Description2"/></td><td><xsl:value-of select="@Description3"/></td></tr></xsl:for-each></xsl:for-each>
<小时>
编辑 2: 这是 3 个嵌套循环的示例,添加了您在最近一次编辑中添加的日期.
<xsl:variable name="name" select="."/><xsl:for-each select="../../DateDetails/Date/DateValue"><xsl:variable name="dateval" select="."/><xsl:for-each select="../../../Descriptions/Description"><tr><td><xsl:value-of select="../../@id"/></td><td><xsl:value-of select="../../@date"/></td><td><xsl:value-of select="../../Gender"/></td><td><xsl:value-of select="$name/@NameType"/></td><td><xsl:value-of select="$name/NameValue/FirstName"/></td><td><xsl:value-of select="$name/NameValue/Surname"/></td><td><xsl:value-of select="@Description1"/></td><td><xsl:value-of select="@Description2"/></td><td><xsl:value-of select="@Description3"/></td><td><xsl:value-of select="$dateval/../@DateType"/></td><td><xsl:value-of select="$dateval/@Year"/></td></tr></xsl:for-each></xsl:for-each></xsl:for-each>
I have a XML file want to convert to a Table OR CSV by XSLT. I tried by Altova MapForce 2014to map the columns but still,not able to gen out the outcomes. thanks a lot.
XML file I have
<Records>
<Person id="756252" date="15-Oct-2014">
<Gender>Male</Gender>
<NameDetails>
<Name NameType="Primary Name">
<NameValue>
<FirstName>Ken</FirstName>
<Surname>Wu</Surname>
</NameValue>
</Name>
<Name NameType="AKA">
<NameValue>
<FirstName>Kenneth</FirstName>
<Surname>Wu</Surname>
</NameValue>
</Name>
<Name NameType="AKA2">
<NameValue>
<FirstName>CAN</FirstName>
<Surname>Wu</Surname>
</NameValue>
</Name>
</NameDetails>
<Descriptions>
<Description Description1="11" Description2="12" Description3="13"/>
<Description Description1="21" Description2="22" Description3="23"/>
<Description Description1="31" Description2="32" Description3="33"/>
</Descriptions>
<DateDetails>
<Date DateType="Date of Birth">
<DateValue Year="1990" />
<DateValue Year="1991" />
</Date>
<Date DateType="Date of Issue">
<DateValue Year="2000" />
<DateValue Year="2001" />
</Date>
</DateDetails>
</Person>
<Person id="253555" date="14-Oct-2014">
<Gender>Male</Gender>
<NameDetails>
<Name NameType="Primary Name">
<NameValue>
<FirstName>Peter</FirstName>
<Surname>Lai</Surname>
</NameValue>
</Name>
</NameDetails>
<Descriptions>
<Description Description1="11" Description2="12" Description3="13"/>
<Description Description1="21" Description2="22"/>
</Descriptions>
<Date DateType="Date of Birth">
<DateValue Year="1992" />
</Date>
</Person>
</Records>
expected outcome
|ID |Date |Gender |NameType |FirstName |SurName |Description1 |Description2 |Description3 |DateType |Year|
|756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |11 |12 |13 |Date of Birth |1990|
|756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |21 |22 |23 |Date of Birth |1990|
|756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |31 |32 |33 |Date of Birth |1990|
|756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |11 |12 |13 |Date of Birth |1990|
|756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |21 |22 |23 |Date of Birth |1990|
|756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |31 |32 |33 |Date of Birth |1990|
|756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |11 |12 |13 |Date of Birth |1990|
|756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |21 |22 |23 |Date of Birth |1990|
|756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |31 |32 |33 |Date of Birth |1990|
|756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |11 |12 |13 |Date of Issue |1991|
|756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |21 |22 |23 |Date of Issue |1991|
|756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |31 |32 |33 |Date of Issue |1991|
|756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |11 |12 |13 |Date of Issue |1991|
|756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |21 |22 |23 |Date of Issue |1991|
|756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |31 |32 |33 |Date of Issue |1991|
|756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |11 |12 |13 |Date of Issue |1991|
|756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |21 |22 |23 |Date of Issue |1991|
|756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |31 |32 |33 |Date of Issue |1991|
|756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |11 |12 |13 |Date of Issue |2000|
|756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |21 |22 |23 |Date of Issue |2000|
|756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |31 |32 |33 |Date of Issue |2000|
|756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |11 |12 |13 |Date of Issue |2000|
|756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |21 |22 |23 |Date of Issue |2000|
|756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |31 |32 |33 |Date of Issue |2000|
|756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |11 |12 |13 |Date of Issue |2000|
|756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |21 |22 |23 |Date of Issue |2000|
|756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |31 |32 |33 |Date of Issue |2000|
|756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |11 |12 |13 |Date of Issue |2001|
|756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |21 |22 |23 |Date of Issue |2001|
|756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |31 |32 |33 |Date of Issue |2001|
|756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |11 |12 |13 |Date of Issue |2001|
|756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |21 |22 |23 |Date of Issue |2001|
|756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |31 |32 |33 |Date of Issue |2001|
|756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |11 |12 |13 |Date of Issue |2001|
|756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |21 |22 |23 |Date of Issue |2001|
|756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |31 |32 |33 |Date of Issue |2001|
|253555 |14-Oct-2014 |Male |Primary Name |Peter |Lai |11 |12 |13 |Date of Issue |1992|
|253555 |14-Oct-2014 |Male |Primary Name |Peter |Lai |21 |22 | |Date of Issue |1992|
XSL i create for testing
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title>Records</title>
</head>
<body>
<table border="1">
<tr>
<th>ID</th>
<th>date</th>
<th>Gender</th>
<th>NameType</th>
<th>FirstName</th>
<th>SurName</th>
<th>Description1</th>
<th>Description2</th>
<th>Description3</th>
</tr>
<xsl:for-each select="Records/Person/NameDetails/Name">
<tr>
<td><xsl:value-of select="../../@id"/></td>
<td><xsl:value-of select="../../@date"/></td>
<td><xsl:value-of select="../../Gender"/></td>
<td><xsl:value-of select="@NameType"/></td>
<td><xsl:value-of select="NameValue/FirstName"/></td>
<td><xsl:value-of select="NameValue/Surname"/></td>
<td><xsl:value-of select="../../Descriptions/Description/@Description1"/></td>
<td><xsl:value-of select="../../Descriptions/Description/@Description2"/></td>
<td><xsl:value-of select="../../Descriptions/Description/@Description3"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
解决方案
You need two nested loops here:
...
<xsl:for-each select="Records/Person/NameDetails/Name">
<xsl:variable name="NameType" select="@NameType"/>
<xsl:variable name="FirstName" select="NameValue/FirstName"/>
<xsl:variable name="Surname" select="NameValue/Surname"/>
<xsl:for-each select="../../Descriptions/Description">
<tr>
<td><xsl:value-of select="../../@id"/></td>
<td><xsl:value-of select="../../@date"/></td>
<td><xsl:value-of select="../../Gender"/></td>
<td><xsl:value-of select="$NameType"/></td>
<td><xsl:value-of select="$FirstName"/></td>
<td><xsl:value-of select="$Surname"/></td>
<td><xsl:value-of select="@Description1"/></td>
<td><xsl:value-of select="@Description2"/></td>
<td><xsl:value-of select="@Description3"/></td>
</tr>
</xsl:for-each>
</xsl:for-each>
...
Edit:
To avoid storing all name details in individual variables, you can do:
<xsl:for-each select="Records/Person/NameDetails/Name">
<xsl:variable name="name" select="."/>
<xsl:for-each select="../../Descriptions/Description">
<tr>
<td><xsl:value-of select="../../@id"/></td>
<td><xsl:value-of select="../../@date"/></td>
<td><xsl:value-of select="../../Gender"/></td>
<td><xsl:value-of select="$name/@NameType"/></td>
<td><xsl:value-of select="$name/NameValue/FirstName"/></td>
<td><xsl:value-of select="$name/NameValue/Surname"/></td>
<td><xsl:value-of select="@Description1"/></td>
<td><xsl:value-of select="@Description2"/></td>
<td><xsl:value-of select="@Description3"/></td>
</tr>
</xsl:for-each>
</xsl:for-each>
Edit 2:
Here's an example of 3 nested loops, adding the dates you have added in your latest edit.
<xsl:for-each select="Records/Person/NameDetails/Name">
<xsl:variable name="name" select="."/>
<xsl:for-each select="../../DateDetails/Date/DateValue">
<xsl:variable name="dateval" select="."/>
<xsl:for-each select="../../../Descriptions/Description">
<tr>
<td><xsl:value-of select="../../@id"/></td>
<td><xsl:value-of select="../../@date"/></td>
<td><xsl:value-of select="../../Gender"/></td>
<td><xsl:value-of select="$name/@NameType"/></td>
<td><xsl:value-of select="$name/NameValue/FirstName"/></td>
<td><xsl:value-of select="$name/NameValue/Surname"/></td>
<td><xsl:value-of select="@Description1"/></td>
<td><xsl:value-of select="@Description2"/></td>
<td><xsl:value-of select="@Description3"/></td>
<td><xsl:value-of select="$dateval/../@DateType"/></td>
<td><xsl:value-of select="$dateval/@Year"/></td>
</tr>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
这篇关于如何使用 XSLT 将 XML 转换为表 [代码更新 11/6]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文