感谢How to use XSLT to convert a XML to Table [CODE UPDATED 11/6],现在我可以通过XSLT将xml转换为表,但是当我添加一个嵌套循环时,结果没有完全加载,我不确定出了什么问题。
数据XML
<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>
<RoleDetail>
<Roles RoleType="Primary">
<OccTitle SinceDay="17" SinceMonth="Nov" SinceYear="2009" OccCat="6">Thai</OccTitle>
</Roles>
</RoleDetail>
<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>
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="/">
<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>
<th>RoleType</th>
<th>OccTitle</th>
<th>SinceDay</th>
<th>SinceMonth</th>
<th>DateType</th>
<th>Year</th>
</tr>
<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="../../../RoleDetail/Roles/OccTitle">
<xsl:variable name="roleval" 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="$roleval/../@RoleType"/>
</td>
<td>
<xsl:value-of select="$roleval/OccTitle"/>
</td>
<td>
<xsl:value-of select="$roleval/OccTitle/@SinceDay"/>
</td>
<td>
<xsl:value-of select="$roleval/OccTitle/@SinceMonth"/>
</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>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我的预期结果
ID |Date |Gender |NameType |FirstName |SurName |Description1 |Description2 |Description3 |RoleType |OccTitle |SinceDay |SinceMonth |DateType |Year
756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |11 |12 |13 |Primary |Thai |17 |Nov |Date of Birth |1990
756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |21 |22 |23 |Primary |Thai |17 |Nov |Date of Birth |1990
756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |31 |32 |33 |Primary |Thai |17 |Nov |Date of Birth |1990
756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |11 |12 |13 |Primary |Thai |17 |Nov |Date of Birth |1991
756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |21 |22 |23 |Primary |Thai |17 |Nov |Date of Birth |1991
756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |31 |32 |33 |Primary |Thai |17 |Nov |Date of Birth |1991
756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |11 |12 |13 |Primary |Thai |17 |Nov |Date of Issue |2000
756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |21 |22 |23 |Primary |Thai |17 |Nov |Date of Issue |2000
756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |31 |32 |33 |Primary |Thai |17 |Nov |Date of Issue |2000
756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |11 |12 |13 |Primary |Thai |17 |Nov |Date of Issue |2001
756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |21 |22 |23 |Primary |Thai |17 |Nov |Date of Issue |2001
756252 |15-Oct-2014 |Male |Primary Name |Ken |Wu |31 |32 |33 |Primary |Thai |17 |Nov |Date of Issue |2001
756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |11 |12 |13 |Primary |Thai |17 |Nov |Date of Birth |1990
756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |21 |22 |23 |Primary |Thai |17 |Nov |Date of Birth |1990
756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |31 |32 |33 |Primary |Thai |17 |Nov |Date of Birth |1990
756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |11 |12 |13 |Primary |Thai |17 |Nov |Date of Birth |1991
756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |21 |22 |23 |Primary |Thai |17 |Nov |Date of Birth |1991
756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |31 |32 |33 |Primary |Thai |17 |Nov |Date of Birth |1991
756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |11 |12 |13 |Primary |Thai |17 |Nov |Date of Issue |2000
756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |21 |22 |23 |Primary |Thai |17 |Nov |Date of Issue |2000
756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |31 |32 |33 |Primary |Thai |17 |Nov |Date of Issue |2000
756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |11 |12 |13 |Primary |Thai |17 |Nov |Date of Issue |2001
756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |21 |22 |23 |Primary |Thai |17 |Nov |Date of Issue |2001
756252 |15-Oct-2014 |Male |AKA |Kenneth |Wu |31 |32 |33 |Primary |Thai |17 |Nov |Date of Issue |2001
756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |11 |12 |13 |Primary |Thai |17 |Nov |Date of Birth |1990
756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |21 |22 |23 |Primary |Thai |17 |Nov |Date of Birth |1990
756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |31 |32 |33 |Primary |Thai |17 |Nov |Date of Birth |1990
756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |11 |12 |13 |Primary |Thai |17 |Nov |Date of Birth |1991
756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |21 |22 |23 |Primary |Thai |17 |Nov |Date of Birth |1991
756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |31 |32 |33 |Primary |Thai |17 |Nov |Date of Birth |1991
756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |11 |12 |13 |Primary |Thai |17 |Nov |Date of Issue |2000
756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |21 |22 |23 |Primary |Thai |17 |Nov |Date of Issue |2000
756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |31 |32 |33 |Primary |Thai |17 |Nov |Date of Issue |2000
756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |11 |12 |13 |Primary |Thai |17 |Nov |Date of Issue |2001
756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |21 |22 |23 |Primary |Thai |17 |Nov |Date of Issue |2001
756252 |15-Oct-2014 |Male |AKA2 |CAN |Wu |31 |32 |33 |Primary |Thai |17 |Nov |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:for-each select="../../../RoleDetail/Roles/OccTitle">
<xsl:variable name="roleval" select="."/>
...
</xsl:for-each>
这个用于数据,这个节点可能会也可能不会进入(XML数据文件),如果这个节点没有进入XML,只需将其留空,否则它也应该循环。
<RoleDetail>
<Roles RoleType="Primary">
<OccTitle SinceDay="17" SinceMonth="Nov" SinceYear="2009" OccCat="6">Thai</OccTitle>
</Roles>
</RoleDetail>
现在,问题是&#34;最后两行&#34;没有显示和Col OccTitle,SinceDay和SinceMonth也是空的 非常感谢
答案 0 :(得分:0)
执行此操作时:
<xsl:for-each select="../../../RoleDetail/Roles/OccTitle">
<xsl:variable name="roleval" select="."/>
$roleval
变量绑定到OccTitle
元素。因此,您需要更改这些:
<td>
<xsl:value-of select="$roleval/OccTitle"/>
</td>
<td>
<xsl:value-of select="$roleval/OccTitle/@SinceDay"/>
</td>
<td>
<xsl:value-of select="$roleval/OccTitle/@SinceMonth"/>
</td>
为:
<td>
<xsl:value-of select="$roleval"/>
</td>
<td>
<xsl:value-of select="$roleval/@SinceDay"/>
</td>
<td>
<xsl:value-of select="$roleval/@SinceMonth"/>
</td>
恕我直言,您应该将变量$occTitle
命名为更清晰,从而避免这种错误。
此节点可能会或可能不会(XML数据文件)
我担心这是一个完全不同的问题。你在这里有一种多维矩阵 - 如果其中一个维度是0,那么整个矩阵将是空的。