multi nested loops by XSL to convert a XML to Table

☆樱花仙子☆ 提交于 2020-01-15 12:21:07

问题


Thanks for How to use XSLT to convert a XML to Table [CODE UPDATED 11/6], now I am able to convert xml to table by XSLT, however when I add in one more nested loop, the result is not fully loaded, I am not sure what went wrong.

Data 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 file

<?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>

my expected outcome

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

the nested loop I added in is

<xsl:for-each select="../../../RoleDetail/Roles/OccTitle">
    <xsl:variable name="roleval" select="."/>
...
</xsl:for-each>

this one is for data, this node may or may not come in (XML data file), if this node is not coming in XML, just leave it empty, otherwise it should be loop also.

<RoleDetail>
<Roles RoleType="Primary">
<OccTitle SinceDay="17" SinceMonth="Nov" SinceYear="2009" OccCat="6">Thai</OccTitle>
</Roles>
</RoleDetail>

Now, the prob is "last TWO lines" is not showing and Col OccTitle,SinceDay and SinceMonth also empty thanks a lot


回答1:


When you do this:

<xsl:for-each select="../../../RoleDetail/Roles/OccTitle">
    <xsl:variable name="roleval" select="."/>

the $roleval variable is bound to the OccTitle element. Therefore, you need to change these:

<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>

to:

<td>
    <xsl:value-of select="$roleval"/>
</td>
<td>
    <xsl:value-of select="$roleval/@SinceDay"/>
</td>
<td>
    <xsl:value-of select="$roleval/@SinceMonth"/>
</td>

IMHO, you should have named the variable $occTitle to make it more clear and thus avoid this mistake.


this node may or may not come in (XML data file)

I am afraid this is a whole different question. What you have here is a kind of a multi-dimensional matrix - if one of the dimensions is 0, then the entire matrix will be empty.



来源:https://stackoverflow.com/questions/24177357/multi-nested-loops-by-xsl-to-convert-a-xml-to-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!