0

単純な 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>

Airbus と等しい場合に、plane/name ノードの子要素テキストを含む順不同リストを作成したいと考えています。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

誰かが私が間違っているところを教えてもらえますか?

4

1 に答える 1

1

まず、「リテラル結果要素」と「属性値テンプレート」が何であるかを学ぶ必要があります。これにより、XSLT コードがより簡潔になります。

あなたの質問に答えるには: 要素を呼び出すapply-templatesが、plane要素に一致するテンプレートがあるため、コードは機能しませんnameplane要素に一致するテンプレートを試してください:

<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>
于 2013-02-14T23:57:33.943 に答える