多嵌套循环通过XSL转换一个XML表(multi nested loops by XSL to co

2019-10-20 07:06发布

感谢您如何使用XSLT的XML转换表[CODE修订11/6] ,现在我可以当我在一个多个嵌套循环添加到XML通过XSLT转换为表格,然而,结果是不满载,我我不知道哪里出了问题。

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>

现在,概率是“最后两行”无法显示和Col OccTitle,SinceDay和SinceMonth也是空非常感谢

Answer 1:

当你这样做:

<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,那么整个矩阵将是空的。



文章来源: multi nested loops by XSL to convert a XML to Table
标签: xml xslt