Xml 使用XSLT从Xpath查询创建无序列表

Xml 使用XSLT从Xpath查询创建无序列表,xml,xslt,xpath,Xml,Xslt,Xpath,当XSLT是一个直接的XPath查询时,我可以在XSLT中创建一个列表,但是当我想创建一个包含所有子元素的列表,其中name=something时,我无法让它工作。以下是我的XML: <?xml version="1.0" encoding="UTF-8"?> <flights xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="flights

当XSLT是一个直接的XPath查询时,我可以在XSLT中创建一个列表,但是当我想创建一个包含所有子元素的列表,其中name=something时,我无法让它工作。以下是我的XML:

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

<flights
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="flights.xsd">

<flight flightid="1">
    <flightno>EK98</flightno>
    <callsign>UAE98</callsign>
    <airline>Emirates Airline</airline>

    <plane planeid="1">
        <name>Airbus</name>
        <registereddate>07-06-10</registereddate>
    </plane>

    <registration>3A6-EDJ</registration>
    <altitude height="feet">41000</altitude>
    <speed ratio="mph">564</speed>
    <distance unit="miles">erf</distance>

    <route>
    <routename>FCO-DXB</routename>
        <from>
            <iatacode>FCO</iatacode>
            <airport>Fiumicino</airport>
            <country>Italy</country>
            <city>Rome</city>
            <latitude>41.8044</latitude>
            <longitude>12.2508</longitude>
        </from>

        <to>
            <iatacode>DXB</iatacode>
            <airport>Dubai Intl</airport>
            <country>UAE</country>
            <city>Dubai</city>
            <latitude>25.2528</latitude>
            <longitude>55.3644</longitude>
        </to>
    </route>

    <course bearing="degrees">154</course>

    <journey>
        <distance type="miles">2,697</distance> 
        <time>PT5H30M</time>
    </journey>

</flight>

有人能告诉我哪里出了问题吗?

首先,您应该了解什么是“文字结果元素”和“属性值模板”。它们将使XSLT代码更加简洁

回答您的问题:您的代码不起作用,因为您在
平面
元素上调用了
应用模板
,但有一个与
名称
元素匹配的模板。尝试与
平面
元素匹配的模板:

<xsl:template match="/">
    <html>
        <head>
            <title>flights</title>
        </head>

        <body>
            <ul style="width:100px; margin:0 auto; padding: 0;">
                <xsl:apply-templates select="flights/flight/plane[name='Airbus']"/> 
            </ul>
        </body>
    </html>
</xsl:template>

<xsl:template match="plane">
    <li style="list-style-type:none; width:100px; margin:0 auto;">
        <xsl:value-of select="name"/>
    </li>
</xsl:template>

飞行
宽度:100px;边距:0自动;填充:0;“>


  • 谢谢你的建议,它帮我解决了问题!
    Name: Airbus
    Registered Date: 07-06-10
    
    <xsl:template match="/">
        <html>
            <head>
                <title>flights</title>
            </head>
    
            <body>
                <ul style="width:100px; margin:0 auto; padding: 0;">
                    <xsl:apply-templates select="flights/flight/plane[name='Airbus']"/> 
                </ul>
            </body>
        </html>
    </xsl:template>
    
    <xsl:template match="plane">
        <li style="list-style-type:none; width:100px; margin:0 auto;">
            <xsl:value-of select="name"/>
        </li>
    </xsl:template>