Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用XSLT和XML创建HTML表,选择一组特定的元素?_Xml_Xslt - Fatal编程技术网

使用XSLT和XML创建HTML表,选择一组特定的元素?

使用XSLT和XML创建HTML表,选择一组特定的元素?,xml,xslt,Xml,Xslt,我需要从XSLT文件创建一个html表,但只选择一组特定的节点。例如,在以下XML中,我希望选择routename包含fco dxb的所有from和to子节点: <?xml version="1.0" encoding="UTF-8"?> <flights xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="flights.xsd">

我需要从XSLT文件创建一个html表,但只选择一组特定的节点。例如,在以下XML中,我希望选择routename包含fco dxb的所有from和to子节点:

<?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 A380-861</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>United Arab Emirates</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>


<flight flightid="2">
    <flightno>BA283</flightno>
    <callsign>BAW283</callsign>
    <airline>British Airways</airline>

    <plane planeid="2">
        <name>Boeing 747-436</name>
        <registereddate>06-12-97</registereddate>
    </plane>


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

    <route>
    <routename>LHR-LAX</routename>
        <from>
            <iatacode>LHR</iatacode>
            <airport>Heathrow</airport>
            <country>England</country>
            <city>London</city>
            <latitude>51.4775</latitude>
            <longitude>0.4614</longitude>
        </from>

        <to>
            <iatacode>LAX</iatacode>
            <airport>Los Angeles International</airport>
            <country>United States of America</country>
            <city>L.A</city>
            <latitude>33.9471</latitude>
            <longitude>-118.4082</longitude>
        </to>
    </route>

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

    <journey>
        <distance type="miles">5,441 miles</distance> 
        <time>PT11H5M</time>
    </journey>

</flight>




</flights>

EK98
阿联酋98
阿联酋航空公司
空中客车A380-861
07-06-10
3A6-EDJ
41000
564
erf
FCO-DXB
FCO
菲米奇诺
意大利
罗马
41.8044
12.2508
DXB
迪拜国际酒店
阿拉伯联合酋长国
迪拜
25.2528
55.3644
154
2,697 
PT5H30M
BA283
BAW283
英国航空公司
波音747-436
06-12-97
3A6-EDJ
41000
564
erf
LHR-LAX
LHR
希思罗机场
英格兰
伦敦
51.4775
0.4614
松懈的
洛杉矶国际酒店
美利坚合众国
洛杉矶
33.9471
-118.4082
154
5441英里
PT11H5M
并将其输出到表中,以下是我在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="table"><xsl:attribute name="border">1</xsl:attribute>
        <xsl:element name="tr">
          <xsl:element name="td">
            <xsl:element name="b">Title</xsl:element>
          </xsl:element>
          <xsl:element name="td">
            <xsl:element name="b">Artist</xsl:element>
          </xsl:element>
          <xsl:element name="td">
            <xsl:element name="b">Year</xsl:element>
          </xsl:element>
        </xsl:element>
        <xsl:apply-templates select="flights/flight/route[contains(text(), 'FCO-DXB')]" />
      </xsl:element>
    </xsl:element>
</xsl:element>
</xsl:template>


<xsl:template match="from">
<xsl:element name="tr">
  <xsl:element name="td"><xsl:value-of select="iatacode"/></xsl:element>
  <xsl:element name="td"><xsl:value-of select="airport"/></xsl:element>
  <xsl:element name="td"><xsl:value-of select="country"/></xsl:element>
  <xsl:element name="td"><xsl:value-of select="city"/></xsl:element>
</xsl:element>
</xsl:template>

<xsl:template match="to">
<xsl:element name="tr">
<xsl:element name="td"><xsl:value-of select="iatacode"/></xsl:element>
  <xsl:element name="td"><xsl:value-of select="airport"/></xsl:element>
  <xsl:element name="td"><xsl:value-of select="country"/></xsl:element>
  <xsl:element name="td"><xsl:value-of select="city"/></xsl:element>
</xsl:element>
</xsl:template>


</xsl:stylesheet>

飞行
1.
标题
艺术家
年

我的目标是最终找出如何将参数传递给XSLT文件,这样我就可以传递一个参数,而不是硬编码包含的文本,但到目前为止,我只想知道如何回答这个问题。

您没有说是否可以有多个具有相同标识符的routename。如果有,它们应该显示在同一个表中,还是显示在不同的表中(每个匹配一个)

我在这里假设,如果有多个具有相同标识符的RouteName,则这些RouteName打印在同一个表中(如果此假设不正确,请纠正我,我将相应地更改代码)

这是实现您想要实现的目标的一种方法:

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

<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" omit-xml-declaration="yes" />

<xsl:param name="code"
           select="'FCO-DXB'" />

<xsl:template match="/flights">
    <html>
        <head>
            <title>flights</title>
        </head>
        <body>
            <xsl:apply-templates select="flight/route[routename/. = $code]" />
        </body>
    </html>
</xsl:template>


<xsl:template match="route">
    <table>
        <tr>
            <td><b>IATA Code</b></td>
            <td><b>Airport</b></td>
            <td><b>Country</b></td>
            <td><b>City</b></td>
        </tr>
        <xsl:apply-templates select="*" />
    </table>
</xsl:template>

<xsl:template match="from|to">
    <tr><xsl:apply-templates select="*" /></tr>
</xsl:template>

<xsl:template match="iatacode|airport|country|city">
    <td><xsl:value-of select="." /></td>
</xsl:template>

<xsl:template match="text()" />

</xsl:stylesheet>

飞行
二字代码
机场
国家
城市

谢谢您的回答。抱歉,只有一个routename包含该文本。您的代码执行了我想要的操作,但它创建了两次表头,我希望所有信息都在一个表中,这可能吗?非常感谢,您的代码比我的代码好多了!:)这应该如预期的那样起作用。我还更新了该解决方案以接受param,该值可以在解析XML之前由XSLT处理器设置(取决于XSLT处理器)。谢谢,我将尝试一下!我添加了完整的XML文件。基本上,我只需要一个包含所选routename的所有from数据和to数据的表。非常感谢。我更新了答案,所以现在它适应了更新的XML文件。