【问题标题】:Create an unordered list from an Xpath query using XSLT使用 XSLT 从 Xpath 查询创建无序列表
【发布时间】:2013-02-14 22:46:57
【问题描述】:

当它是一个直接的 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 文件中的尝试:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="yes" />

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

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

            </xsl:element>
        </xsl:element>
    </xsl:element>

</xsl:template>


<xsl:template match="name">
    <xsl:element name="li">
        <xsl:attribute name="style">
            <xsl:text>list-style-type:none; width:100px; margin:0 auto;</xsl:text>
        </xsl:attribute>

    <xsl:value-of select="name" />

    </xsl:element>
</xsl:template>



</xsl:stylesheet>

所以结果会是这样的:

Name: Airbus
Registered Date: 07-06-10

谁能告诉我哪里出错了?

【问题讨论】:

    标签: xml xslt xpath


    【解决方案1】:

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

    回答您的问题:您的代码不起作用,因为您在 plane 元素上调用了 apply-templates,但有一个与 name 元素匹配的模板。尝试匹配plane 元素的模板:

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

    【讨论】:

    • 感谢您的建议,它帮助我解决了问题!
    猜你喜欢
    • 2015-11-15
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多