XML显示多次出现的嵌套元素

时间:2015-04-09 00:02:57

标签: xml xslt

我对此非常麻烦。我正在尝试创建一个文档,显示4个不同航班的座位信息(我使用表格)...按照我的方式,只显示座位信息的第一个实例(我只有"现在是XSL中的行"元素,直到我能弄明白如何让它表现出来!!!),而不是通过并显示所有这些元素。这是XML的一部分(我不会发布整个内容,因为它非常大):

 <flights xmlns:BOS="www.boston.com" xmlns:SFO="www.sanfrancisco.com" xmlns:CHI="www.chicago.com" xmlns:HAW="www.hawaii.com" xmlns:LAX="www.losangeles.com">
 <BOS>               
    <orig>BOS</orig>
    <dest>SFO</dest>
    <etd>12pm</etd>
    <eta>3:30pm</eta>
    <num>1185</num>


    <crft>
        Boeing 757
    </crft>

    <crew>
        <pilot>
            <pltfname>James</pltfname>
            <pltlname>Kirk</pltlname>
        </pilot>
        <copilot>
            <copltfname>Mister</copltfname>
            <copltlname>Spock</copltlname>
        </copilot>
    </crew>

    <seating>
        <frstcls cls="Business First" >           <!-- BOS to SFO 1st Class -->  
            <seat>
                <pas>
                    <fname>Unoccupied</fname>
                    <lname>Unoccupied</lname>
                    <age></age>
                </pas>
                <stnum>
                    <row>1</row>
                    <aisle>A</aisle>
                </stnum>
            </seat>
            <seat>
                <pas>
                    <fname>Unoccupied</fname>
                    <lname>Unoccupied</lname>
                    <age></age>
                </pas>
                <stnum>
                    <row>1</row>
                    <aisle>B</aisle>
                </stnum>
            </seat>
            <seat>
                <pas>
                    <fname>Janice</fname>
                    <lname>Rand</lname>
                    <age>adult</age>
           </pas>
                <stnum>
                    <row>1</row>
                    <aisle>E</aisle>
                </stnum>
            </seat>
            <seat>
                <pas>
                    <fname>Unoccupied</fname>
                    <lname>Unoccupied</lname>
                    <age></age>
                </pas>
                <stnum>
                    <row>1</row>
                    <aisle>F</aisle>
                </stnum>
            </seat>

            <seat>
                <pas>
                    <fname>Unoccupied</fname>
                    <lname>Unoccupied</lname>
                    <age></age>
                </pas>
                <stnum>
                    <row>2</row>
                    <aisle>A</aisle>
                </stnum>
            </seat>
            <seat>
                <pas>
                    <fname>Unoccupied</fname>
                    <lname>Unoccupied</lname>
                    <age></age>
                </pas>
                <stnum>
                    <row>2</row>
                    <aisle>B</aisle>
                </stnum>
            </seat>
            <seat>
                <pas>
                    <fname>Unoccupied</fname>
                    <lname>Unoccupied</lname>
                    <age></age>
                </pas>
                <stnum>
                    <row>2</row>
                    <aisle>E</aisle>
                </stnum>
            </seat>
            <seat>
                <pas>
                    <fname>William</fname>
                    <lname>McCoy</lname>
                    <age>adult</age>
                </pas>
                <stnum>
                    <row>2</row>
                    <aisle>F</aisle>
                </stnum>
            </seat>

            <seat>
                <pas>
                    <fname>Unoccupied</fname>
                    <lname>Unoccupied</lname>
                    <age></age>
                </pas>
                <stnum>
                    <row>3</row>
                    <aisle>A</aisle>
                </stnum>
            </seat>
            <seat>
                <pas>
                    <fname>Montgomery </fname>
                    <lname>Scott</lname>
                    <age>adult</age>
                </pas>
                <stnum>
                    <row>3</row>
                    <aisle>B</aisle>
                </stnum>
            </seat>
            <seat>
                <pas>
                    <fname>Unoccupied</fname>
                    <lname>Unoccupied</lname>
                    <age></age>
                </pas>
                <stnum>
                    <row>3</row>
                    <aisle>E</aisle>
                </stnum>
            </seat>
            <seat>
                <pas>
                    <fname>Diane</fname>
                    <lname>Chapel</lname>
                    <age>adult</age>
                </pas>
                <stnum>
                    <row>3</row>
                    <aisle>F</aisle>
                </stnum>
            </seat>

            <seat>
                <pas>
                    <fname>Unoccupied</fname>
                    <lname>Unoccupied</lname>
                    <age></age>
                </pas>
                <stnum>
                    <row>4</row>
                    <aisle>A</aisle>
                </stnum>
            </seat>
            <seat>
                <pas>
                    <fname>Unoccupied</fname>
                    <lname>Unoccupied</lname>
                    <age></age>
                </pas>
                <stnum>
                    <row>4</row>
                    <aisle>B</aisle>
                </stnum>
            </seat>
            <seat>
                <pas>
                    <fname>Unoccupied</fname>
                    <lname>Unoccupied</lname>
                    <age></age>
                </pas>
                <stnum>
                    <row>4</row>
                    <aisle>E</aisle>
                </stnum>
            </seat>
            <seat>
                <pas>
                    <fname>Unoccupied</fname>
                    <lname>Unoccupied</lname>
                    <age></age>
                </pas>
                <stnum>
                    <row>4</row>
                    <aisle>F</aisle>
                </stnum>
            </seat>
        </frstcls>
 </seating>

这是我的XSL文件:

<html>
    <body style="font-family:verdana;">
        <h1>United Airlines</h1>




            <table border="1">

            <xsl:for-each select="BOS">

                <tr><th>Flight No.</th><th>Origin</th><th>ETD</th><th>Destination</th><th>ETA</th></tr>

                <tr>
                    <td><xsl:value-of select="num"/></td>
                    <td><xsl:value-of select="orig"/></td>
                    <td><xsl:value-of select="etd"/></td>
                    <td><xsl:value-of select="dest"/></td>
                    <td><xsl:value-of select="eta"/></td>
                </tr>

                <tr><th colspan="5">Aircraft Type</th></tr>
                <tr><td colspan="5"><xsl:value-of select="crft"/></td></tr>


                <tr>
                    <th colspan="5">Crew</th>
                </tr> 

                <tr>
                    <th colspan="2">Pilot</th>
                <td><xsl:value-of select="crew/pilot/pltfname"/></td>
                <td colspan="2"><xsl:value-of select="crew/pilot/pltlname"/></td>
                </tr>

                <tr>
                    <th colspan="2">Co-pilot</th>
                <td><xsl:value-of select="crew/copilot/copltfname"/></td>
                <td><xsl:value-of select="crew/copilot/copltlname"/></td>
                </tr>




                <tr>
                    <th style="padding: 12px; font-size:1.5em;" colspan="5">Seating</th>
                </tr>
                <tr>
                    <th style="padding: 5px;">Business First</th>
                </tr>

                <tr>
                    <th colspan="2">Seat</th><th colspan="2">Name</th><th>Adult/Child</th>
                </tr>


                <tr><td><xsl:value-of select="seating/frstcls/seat/stnum/row"/></td></tr>

                 </xsl:for-each>

1 个答案:

答案 0 :(得分:0)

问题是由样式表中的这一行引起的:

<tr><td><xsl:value-of select="seating/frstcls/seat/stnum/row"/></td></tr>

XPath seating/frstcls/seat/stnum/row选择所有row元素,但在XSLT 1.0中,序列的字符串值(= xsl:value-of返回的内容)只是第一项;使用XSLT 2.0仍然无法获得预期的结果,因为在单个单元格中有所有值

您需要做的是使用另一个xsl:for-each

循环访问座位信息
<xsl:for-each select="seating/frstcls/seat">
    <tr>
        <td><xsl:value-of select="stnum/row"/></td>
        <td><!-- another value --></td>
        <td><!-- ... --></td>
    </tr>
</xsl:for-each>