在解析XML时嵌套XSL for-each语句的难度

时间:2016-06-27 04:04:38

标签: xml xslt xml-parsing

我无法读出我的嵌套for-each循环。出于某种原因,它不会在直接节点下循环。问题似乎与tages有关。我想在名称值的所有标签上访问迭代,我能够提取值,但不是特定于父节点。这次演习中我的眼睛充满了血丝,有人可以提供一些帮助。提前致谢。 我希望输出像

输出

主要名称:少将Aslan Mustaq Ibraimis Dze Abashidze

拼写变化:Tarmo Mand                     Tarmo Maend

<?xml version="1.0" encoding="UTF-8"?>

<xsl:template match="/">
    <html>
        <body>
            <h2>People Details</h2>
            <table border="1">
                <tr bgcolor="#4CAF50">
                    <th style="text-align:left" width="15%">Header</th>
                    <th style="text-align:left" width="75%">Details</th>
                </tr>
                <xsl:for-each select="/PFA/Records/Person[@id='90061']">
                    <tr>
                        <td colspan="2"></td>
                    </tr>
                    <tr>
                        <td>Person ID</td>
                        <td>
                            <xsl:value-of select="./@id" />
                        </td>
                    </tr>
                    <tr>
                        <td>Gender</td>
                        <td>
                            <xsl:value-of select="Gender" />
                        </td>
                    </tr>
                    <tr>
                        <td>Name Details </td>
                    </tr>


                    <xsl:for-each select="./NameDetails">
                        <xsl:for-each select="./Name">

                                <tr>
                                    <td>
                                        <xsl:value-of select="@NameType" />

                                    </td>
                                    <td>

                                        <xsl:for-each select="./NameValue">
                                             <xsl:value-of select="TitleHonorific" />
                                             <xsl:text> </xsl:text>
                                            <xsl:value-of select="FirstName" />
                                            <xsl:text> </xsl:text>
                                            <xsl:value-of select="MiddleName" />
                                            <xsl:text>  </xsl:text>
                                            <xsl:value-of select="Surname" />
                                            <!-- <xsl:value-of select="." /> -->
                                        </xsl:for-each>

                                    </td>
                                </tr>

                        </xsl:for-each>
                    </xsl:for-each>

                    <tr>
                        <td>AML List Details</td>
                    </tr>
                    <xsl:for-each select="./Descriptions/Description">
                        <xsl:variable name="vDescription1" select="./@Description1" />
                        <xsl:variable name="vDescription2" select="./@Description2" />
                        <xsl:variable name="vDescription3" select="./@Description3" />
                        <tr>
                            <td></td>
                            <td>
                                <xsl:value-of
                                    select="/PFA/Description1List/Description1Name[@Description1Id=$vDescription1]" />
                                -
                                <xsl:value-of
                                    select="/PFA/Description2List/Description2Name[@Description1Id=$vDescription1 and @Description2Id=$vDescription2]" />
                                -
                                <xsl:value-of
                                    select="/PFA/Description3List/Description3Name[@Description3Id=$vDescription3 and @Description2Id=$vDescription2]" />
                            </td>
                        </tr>
                    </xsl:for-each>
                    <tr>
                        <td>Role Details - Primary Occupation</td>
                    </tr>
                    <xsl:for-each
                        select="./RoleDetail/Roles[@RoleType='Primary Occupation']/OccTitle">
                        <xsl:variable name="vPRoleCat" select="./@OccCat" />
                        <tr>
                            <td></td>
                            <td>
                                <xsl:value-of
                                    select="/PFA/OccupationList/Occupation[@code=$vPRoleCat]/@name" />
                                -
                                <xsl:value-of select="." />
                            </td>
                        </tr>
                    </xsl:for-each>
                    <tr>
                        <td>Role Details - Previous Roles</td>
                    </tr>
                    <xsl:for-each
                        select="./RoleDetail/Roles[@RoleType='Previous Roles']/OccTitle">
                        <xsl:variable name="vPrvRoleCat" select="./@OccCat" />
                        <tr>
                            <td></td>
                            <td>
                                <xsl:value-of
                                    select="/PFA/OccupationList/Occupation[@code=$vPrvRoleCat]/@name" />
                                -
                                <xsl:value-of select="." />
                            </td>
                        </tr>
                    </xsl:for-each>
                    <tr>
                        <td>Country Details</td>
                    </tr>
                    <xsl:for-each select="./CountryDetails/Country">
                        <xsl:variable name="vCode" select="./CountryValue/@Code" />
                        <tr>
                            <td></td>
                            <td>
                                <xsl:value-of select="./@CountryType" />
                                -
                                <xsl:value-of
                                    select="/PFA/CountryList/CountryName[@code=$vCode]/@name" />
                            </td>
                        </tr>
                    </xsl:for-each>
                    <tr>
                        <td>Profile Notes</td>
                    </tr>
                    <tr>
                        <td></td>
                        <td>
                            <xsl:value-of select="./ProfileNotes" />
                        </td>
                    </tr>
                    <tr>
                        <td>Source Details</td>
                    </tr>
                    <xsl:for-each select="./SourceDescription/Source">
                        <tr>
                            <td></td>
                            <td>
                                <xsl:value-of select="./@name" />
                            </td>
                        </tr>
                    </xsl:for-each>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>

<?xml version="1.0" encoding="utf-8"?>

<Records>
    <Person id="90061" action="add" date="31-Mar-2013">
        <Gender>Male</Gender>
        <ActiveStatus>Active</ActiveStatus>
        <Deceased>No</Deceased>
        <NameDetails>
            <Name NameType="Primary Name">
                <NameValue>
                    <TitleHonorific>Major General</TitleHonorific>
                    <FirstName>Aslan Mustaq</FirstName>
                    <MiddleName>Ibraimis Dze</MiddleName>
                    <Surname>Abashidze</Surname>
                    <OriginalScriptName>ასლან იბრაიმის ძე აბაშიძე</OriginalScriptName>
                </NameValue>
            </Name>
            <Name NameType="Spelling Variation">
                <NameValue>
                    <FirstName>Tarmo</FirstName>
                    <Surname>Mand</Surname>
                </NameValue>
                <NameValue>
                    <FirstName>Tarmo</FirstName>
                    <Surname>Maend</Surname>
                </NameValue>
            </Name>
        </NameDetails>
        <Descriptions>
            <Description Description1="1" />
            <Description Description1="3" Description2="2" />
            <Description Description1="3" Description2="6" />
            <Description Description1="3" Description2="7" />
            <Description Description1="3" Description2="8" />
        </Descriptions>
        <RoleDetail>
            <Roles RoleType="Primary Occupation">
                <OccTitle OccCat="13">See Previous Roles</OccTitle>
            </Roles>
            <Roles RoleType="Previous Roles">
                <OccTitle SinceYear="1991" ToDay="06" ToMonth="May"
                    ToYear="2004" OccCat="13">President, Adjara Autonomous Republic
                </OccTitle>
                <OccTitle SinceYear="1990" ToYear="1995" OccCat="3">Deputy
                    Speaker, Parliament</OccTitle>
                <OccTitle SinceYear="1990" ToYear="1995" OccCat="3">Member,
                    Parliament</OccTitle>
                <OccTitle SinceYear="1986" ToYear="1989" OccCat="2">Minister
                    of Civil Service</OccTitle>
                <OccTitle SinceYear="1984" ToYear="1986" OccCat="4">Deputy
                    Minister of Civil Service</OccTitle>
            </Roles>
        </RoleDetail>
        <DateDetails>
            <Date DateType="Date of Birth">
                <DateValue Day="20" Month="Jul" Year="1938" />
            </Date>
        </DateDetails>
        <BirthPlace>
            <Place name="Batumi, Adjara Autonomous Republic, Georgia" />
        </BirthPlace>
        <SanctionsReferences>
            <Reference>1072</Reference>
        </SanctionsReferences>
        <CountryDetails>
            <Country CountryType="Citizenship">
                <CountryValue Code="GRGIA" />
            </Country>
            <Country CountryType="Resident of">
                <CountryValue Code="GRGIA" />
            </Country>
            <Country CountryType="Jurisdiction">
                <CountryValue Code="GRGIA" />
            </Country>
        </CountryDetails>
        <ProfileNotes>Interpol Warning:

            LEGAL STATUS
            Language spoken: Georgian, Russian

            PHYSICAL DESCRIPTION
            Height: 1.68 meter &lt;-&gt; 66 inches
            Colour of eyes: Blue
            Colour of hair: Grey

            OFFENCES
            Offences: Crimes Involving The Use Of Weapons/Explosives, Fraud, Kidnapping,
            Organized Crime /Transnational Crime, Terrorism
            Arrest Warrant Issued by: Batumi / Georgia
        </ProfileNotes>
        <SourceDescription>
            <Source
                name="https://secure.interpol.int/public/Data/Wanted/Notices/Data/2006/90/2006_690.asp" />
        </SourceDescription>
        <Images>
            <Image URL="http://kavkaz.memo.ru/img/cont/a_abashidze.jpg" />
            <Image URL="http://www.ng.ru/images/2002-10-02/209_1_1a.jpg" />
            <Image
                URL="http://img.lenta.ru/vojna/2004/05/06/resignation/picture.jpg" />
            <Image
                URL="https://secure.interpol.int/Public/Data/Wanted/Notices/Images/photo/original/2006/01/09/53450209.jpg" />
        </Images>
    </Person>
    </Records>

1 个答案:

答案 0 :(得分:0)

嘿,您可以测试此XSLT以获得所需的输出:

&#13;
&#13;
<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="utf-8" method="html" omit-xml-declaration="yes"/>
<xsl:template match="/">
    <html>
        <body>
            <h2>People Details</h2>
            <table border="1">
                <tr bgcolor="#4CAF50">
                    <th style="text-align:left" width="15%">Header</th>
                    <th style="text-align:left" width="75%">Details</th>
                </tr>
                <xsl:for-each select="/Records/Person[@id='90061']">
                    <tr>
                        <td colspan="2"></td>
                    </tr>
                    <tr>
                        <td>Person ID</td>
                        <td>
                            <xsl:value-of select="./@id" />
                        </td>
                    </tr>
                    <tr>
                        <td>Gender</td>
                        <td>
                            <xsl:value-of select="Gender" />
                        </td>
                    </tr>
                    <tr>
                        <td>Name Details </td>
                    </tr>


                    <xsl:for-each select="./NameDetails">
                        <xsl:for-each select="./Name">

                                <tr>
                                    <td>
                                        <xsl:value-of select="@NameType" />

                                    </td>
                                    <td>

                                        <xsl:for-each select="./NameValue">
                                             <xsl:value-of select="TitleHonorific" />
                                             <xsl:text> </xsl:text>
                                            <xsl:value-of select="FirstName" />
                                            <xsl:text> </xsl:text>
                                            <xsl:value-of select="MiddleName" />
                                            <xsl:text>  </xsl:text>
                                            <xsl:value-of select="Surname" />
                                            <!-- <xsl:value-of select="." /> -->
                                        </xsl:for-each>

                                    </td>
                                </tr>

                        </xsl:for-each>
                    </xsl:for-each>

                    <tr>
                        <td>AML List Details</td>
                    </tr>
                    <xsl:for-each select="./Descriptions/Description">
                        <xsl:variable name="vDescription1" select="./@Description1" />
                        <xsl:variable name="vDescription2" select="./@Description2" />
                        <xsl:variable name="vDescription3" select="./@Description3" />
                        <tr>
                            <td></td>
                            <td>
                                <xsl:value-of
                                    select="/Records/Description1List/Description1Name[@Description1Id=$vDescription1]" />
                                -
                                <xsl:value-of
                                    select="/Records/Description2List/Description2Name[@Description1Id=$vDescription1 and @Description2Id=$vDescription2]" />
                                -
                                <xsl:value-of
                                    select="/Records/Description3List/Description3Name[@Description3Id=$vDescription3 and @Description2Id=$vDescription2]" />
                            </td>
                        </tr>
                    </xsl:for-each>
                    <tr>
                        <td>Role Details - Primary Occupation</td>
                    </tr>
                    <xsl:for-each
                        select="./RoleDetail/Roles[@RoleType='Primary Occupation']/OccTitle">
                        <xsl:variable name="vPRoleCat" select="./@OccCat" />
                        <tr>
                            <td></td>
                            <td>
                                <xsl:value-of
                                    select="/Records/OccupationList/Occupation[@code=$vPRoleCat]/@name" />
                                -
                                <xsl:value-of select="." />
                            </td>
                        </tr>
                    </xsl:for-each>
                    <tr>
                        <td>Role Details - Previous Roles</td>
                    </tr>
                    <xsl:for-each
                        select="./RoleDetail/Roles[@RoleType='Previous Roles']/OccTitle">
                        <xsl:variable name="vPrvRoleCat" select="./@OccCat" />
                        <tr>
                            <td></td>
                            <td>
                                <xsl:value-of
                                    select="/Records/OccupationList/Occupation[@code=$vPrvRoleCat]/@name" />
                                -
                                <xsl:value-of select="." />
                            </td>
                        </tr>
                    </xsl:for-each>
                    <tr>
                        <td>Country Details</td>
                    </tr>
                    <xsl:for-each select="./CountryDetails/Country">
                        <xsl:variable name="vCode" select="./CountryValue/@Code" />
                        <tr>
                            <td></td>
                            <td>
                                <xsl:value-of select="./@CountryType" />
                                -
                                <xsl:value-of
                                    select="/Records/CountryList/CountryName[@code=$vCode]/@name" />
                            </td>
                        </tr>
                    </xsl:for-each>
                    <tr>
                        <td>Profile Notes</td>
                    </tr>
                    <tr>
                        <td></td>
                        <td>
                            <xsl:value-of select="./ProfileNotes" />
                        </td>
                    </tr>
                    <tr>
                        <td>Source Details</td>
                    </tr>
                    <xsl:for-each select="./SourceDescription/Source">
                        <tr>
                            <td></td>
                            <td>
                                <xsl:value-of select="./@name" />
                            </td>
                        </tr>
                    </xsl:for-each>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>
</xsl:stylesheet>
&#13;
&#13;
&#13;

相关问题