0

我在我的 XML 中存储了一个持续时间,在我的模式中我将它设置为一个持续时间类型:

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

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified" 
attributeFormDefault="unqualified">


<xs:element name="flights">
<xs:complexType>
  <xs:sequence>
    <xs:element name="flight" type="flighttype" minOccurs='1' maxOccurs='unbounded'>
      <xs:unique name="planeidisunique">
        <xs:selector xpath="plane"/>
        <xs:field xpath="@planeid"/>
      </xs:unique>
    </xs:element>
  </xs:sequence>
</xs:complexType>
<xs:unique name="flightidisunique">
  <xs:selector xpath="flight"/>
  <xs:field xpath="@flightid"/>
</xs:unique>
</xs:element>

<xs:complexType name="flighttype">
<xs:sequence>
    <xs:element name="flightno" type="xs:string" minOccurs="1" maxOccurs="1"/>
    <xs:element name="callsign" type="xs:string" minOccurs="1" maxOccurs="1"/>
    <xs:element name="airline" type="xs:string" minOccurs="1" maxOccurs="1"/>
    <xs:element name="plane" type="planetype" minOccurs='1' maxOccurs='unbounded' />
    <xs:element name="route" type="routetype"/>
</xs:sequence>
<xs:attribute name="flightid" type="xs:string"/>
</xs:complexType>

<!-- PLANE -->

<xs:complexType name="planetype" mixed="true">
<xs:sequence>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="speed" type="speedtype"/>
    <xs:element name="wingspan" type="xs:string"/>
    <xs:element name="length" type="xs:string"/>
    <xs:element name="registereddate" type="xs:date"/>
</xs:sequence>
<xs:attribute name="planeid" type="xs:string"/>
</xs:complexType>


<!-- ALTITUDE -->

<xs:complexType name="altitudetype">
<xs:simpleContent>
  <xs:extension base="xs:integer">
    <xs:attribute name="height" type="xs:string">
    </xs:attribute>
  </xs:extension>
</xs:simpleContent>
</xs:complexType>


<!-- SPEED THE VALUE OF SPEED MUST BE AN INTEGER-->

<xs:complexType name="speedtype">
<xs:simpleContent>
  <xs:extension base="xs:integer">
    <xs:attribute name="ratio" type="xs:string">
    </xs:attribute>
  </xs:extension>
</xs:simpleContent>
</xs:complexType>



  <!-- DISTANCE VALUE OF DISTANCE MUST BE AN INTEGER-->

<xs:complexType name="distancetype">
<xs:simpleContent>
  <xs:extension base="xs:integer">
    <xs:attribute name="unit" type="xs:string">
    </xs:attribute>
  </xs:extension>
</xs:simpleContent>
  </xs:complexType>


<!-- COURSE -->

<xs:complexType name="coursetype">
<xs:simpleContent>
  <xs:extension base="xs:integer">
    <xs:attribute name="bearing" type="xs:string">
    </xs:attribute>
  </xs:extension>
</xs:simpleContent>
  </xs:complexType>



  <!-- DURATION -->

<xs:complexType name="durationtype">
<xs:simpleContent>
  <xs:extension base="xs:duration">
  </xs:extension>
</xs:simpleContent>
  </xs:complexType>



<!-- ROUTE -->


<xs:complexType name="routetype">
<xs:sequence>
    <xs:element name="routename" type="xs:string"/>
    <xs:element name="altitude" type="altitudetype" minOccurs="1" maxOccurs="1"/>
    <xs:element name="speed" type="speedtype" minOccurs="0" maxOccurs="1"/>
    <xs:element name="course" type="coursetype"/>
    <xs:element name="distance" type="distancetype"/>
    <xs:element name="duration" type="durationtype"/>
    <xs:element name="from" type="fromtype"/>
    <xs:element name="to" type="totype"/>
</xs:sequence>
</xs:complexType>



<!-- FROM -->

<xs:complexType name="fromtype">
<xs:sequence>
    <xs:element name="iatacode"/>
    <xs:element name="airport"/>
    <xs:element name="country"/>
    <xs:element name="city"/>
    <xs:element name="latitude"/>
    <xs:element name="longitude"/>
    <xs:element name="yahoowoeid"/>
</xs:sequence>
</xs:complexType>

<!-- TO -->

<xs:complexType name="totype">
<xs:sequence>
    <xs:element name="iatacode"/>
    <xs:element name="airport"/>
    <xs:element name="country"/>
    <xs:element name="city"/>
    <xs:element name="latitude"/>
    <xs:element name="longitude"/>
    <xs:element name="yahoowoeid"/>
</xs:sequence>
</xs:complexType>


</xs:schema>

在我的 XML 中,我使用这样的持续时间:

<duration>PT12H37M</duration>

这是我的 XSL 文件,我在使用持续时间的地方添加了注释:

<?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:output doctype-public="-//WAPFORUM//DTD XHTML Mobile 1.0//EN" doctype-system="http://www.wapforum.org/DTD/xhtml-mobile10.dtd"/>

<!-- Parameters for the map longitudes and latitudes and route name-->
<xsl:param name="a" />
<xsl:param name="b" />
<xsl:param name="c" />
<xsl:param name="d" />

<xsl:template match="/">
    <xsl:element name="html">
        <xsl:element name="head">
            <xsl:element name="title">Selected Flight Route</xsl:element>
            <link rel="stylesheet" type="text/css" href="mystyles.css" title="Style"/>
        </xsl:element>

        <xsl:element name="body"> 

            <xsl:element name="div"><!-- This holds the navigation bar-->
                <xsl:attribute name="id">
                    <xsl:text>navdiv</xsl:text>
                </xsl:attribute>


                <xsl:element name="div"><!-- This holds the image links-->
                    <xsl:attribute name="id">
                        <xsl:text>navlinks</xsl:text>
                    </xsl:attribute>

                    <xsl:element name="a">
                        <xsl:attribute name="id">
                            <xsl:text>navlinksone</xsl:text>
                        </xsl:attribute>
                        <xsl:attribute name="href">flights.php</xsl:attribute>
                    </xsl:element>

                    <xsl:element name="a">
                        <xsl:attribute name="id">
                            <xsl:text>navlinkstwo</xsl:text>
                        </xsl:attribute>
                        <xsl:attribute name="href">planes.php</xsl:attribute>
                    </xsl:element>

                    <xsl:element name="a">
                        <xsl:attribute name="id">
                            <xsl:text>navlinksthree</xsl:text>
                        </xsl:attribute>
                        <xsl:attribute name="href">weatherfeed.php</xsl:attribute>
                    </xsl:element>

                </xsl:element>
            </xsl:element><!-- End the navigation bar-->

            <xsl:element name="div">
                <xsl:attribute name="id">mapdiv</xsl:attribute>
                <img width="300" height="270" src="http://maps.google.com/maps/api/staticmap?size=300x270&amp;zoom=1&amp;path=color:0xff0000ff|weight:4|{$a},{$b}|{$c},{$d}
                    &amp;markers=color%3ablue|label%3aA|
                    {$a},{$b}|&amp;markers=color%3agreen|label%3aB|{$c},{$d}
                    &amp;sensor=false" />
            </xsl:element>

            <xsl:element name="hr"></xsl:element>


            <!-- The following is the table which holds all of the map and route information -->
            <table><xsl:attribute name="class">tab</xsl:attribute>
                <tr>
                    <td colspan="3"><xsl:attribute name="class">top</xsl:attribute><xsl:value-of select="/flights/flight/route[routename/. = $code]/routename"/></td>
                    </tr>
                    <tr>
                    <td><xsl:attribute name="class">lside</xsl:attribute></td>
                    <td><xsl:attribute name="class">lside</xsl:attribute>From<img src="bm.png" height="18" width="12"/></td>
                    <td><xsl:attribute name="class">lside</xsl:attribute>To<img src="gm.png" height="18" width="12"/></td>
                    </tr>

                    <tr>
                    <td><xsl:attribute name="class">lside</xsl:attribute>City</td>
                    <td><xsl:attribute name="class">rside</xsl:attribute><xsl:value-of select="/flights/flight/route[routename/. = $code]/from/city"/></td>
                    <td><xsl:attribute name="class">rside</xsl:attribute><xsl:value-of select="/flights/flight/route[routename/. = $code]/to/city"/></td>
                    </tr>

                    <tr>
                    <td><xsl:attribute name="class">lside</xsl:attribute>IATA Code</td>
                    <td><xsl:attribute name="class">rside</xsl:attribute><xsl:value-of select="/flights/flight/route[routename/. = $code]/from/iatacode"/></td>
                    <td><xsl:attribute name="class">rside</xsl:attribute><xsl:value-of select="/flights/flight/route[routename/. = $code]/to/iatacode"/></td>
                    </tr>

                    <tr >
                    <td><xsl:attribute name="class">lside</xsl:attribute>Country</td>
                   <td><xsl:attribute name="class">rside</xsl:attribute><xsl:value-of select="/flights/flight/route[routename/. = $code]/from/country"/></td>
                    <td><xsl:attribute name="class">rside</xsl:attribute><xsl:value-of select="/flights/flight/route[routename/. = $code]/to/country"/></td>
                    </tr>

                    <tr>
                        <td><xsl:attribute name="class">lside</xsl:attribute>Weather</td>


                        <td><xsl:attribute name="class">rside</xsl:attribute>
                            <xsl:element name="a">
                                <xsl:attribute name="id">
                                    <xsl:text>weatherimagelinks</xsl:text>
                                </xsl:attribute>
                            <xsl:attribute name="href">weather.php?woeid=<xsl:value-of select="/flights/flight/route[routename/. = $code]/from/yahoowoeid" /></xsl:attribute>
                            </xsl:element>
                        </td>
                          <td><xsl:attribute name="class">rside</xsl:attribute>
                            <xsl:element name="a">
                                <xsl:attribute name="id">
                                    <xsl:text>weatherimagelinks</xsl:text>
                                </xsl:attribute>
                            <xsl:attribute name="href">weather.php?woeid=<xsl:value-of select="/flights/flight/route[routename/. = $code]/to/yahoowoeid" /></xsl:attribute>
                            </xsl:element>
                        </td>
                    </tr>
                    <tr>
                        <td><xsl:attribute name="class">lside</xsl:attribute>Distance</td>
                        <td colspan="2"><xsl:attribute name="class">rside</xsl:attribute><xsl:value-of select="/flights/flight/route[routename/. = $code]/distance"/><xsl:value-of select="/flights/flight/route[routename/. = $code]/distance/@unit"/></td>
                    </tr>
                    <tr>
                        <td><xsl:attribute name="class">lside</xsl:attribute>Course</td>
                        <td colspan="2"><xsl:attribute name="class">rside</xsl:attribute><xsl:value-of select="/flights/flight/route[routename/. = $code]/course"/><xsl:value-of select="/flights/flight/route[routename/. = $code]/course/@bearing"/></td>
                    </tr>
                    <tr>
                        <td><xsl:attribute name="class">lside</xsl:attribute>Airline</td>
                        <td colspan="2"><xsl:attribute name="class">rside</xsl:attribute><xsl:value-of select="/flights/flight/route[routename/. = $code]/../airline"/></td>
                    </tr>
                    <tr>
                        <td><xsl:attribute name="class">lside</xsl:attribute>Flight No</td>
                        <td colspan="2"><xsl:attribute name="class">rside</xsl:attribute><xsl:value-of select="/flights/flight/route[routename/. = $code]/../flightno"/></td>
                    </tr>
                    <tr>
                        <td><xsl:attribute name="class">lside</xsl:attribute>Call Sign</td>
                        <td colspan="2"><xsl:attribute name="class">rside</xsl:attribute><xsl:value-of select="/flights/flight/route[routename/. = $code]/../callsign"/></td>
                    </tr>
                    <tr>
<!-- THIS IS WHERE I OUTPUT THE DURATION AND I THINK THIS IS WHERE I NEED TO TRANSFORM IT -->
                        <td><xsl:attribute name="class">lside</xsl:attribute>Duration</td>
                        <td colspan="2"><xsl:attribute name="class">rside</xsl:attribute><xsl:value-of select="/flights/flight/route[routename/. = $code]/duration"/></td>
                    </tr>
                    <tr>
                        <td><xsl:attribute name="class">lside</xsl:attribute>Altitude</td>
                        <td colspan="2"><xsl:attribute name="class">rside</xsl:attribute><xsl:value-of select="/flights/flight/route[routename/. = $code]/altitude"/><xsl:value-of select="/flights/flight/route[routename/. = $code]/altitude/@height"/></td>
                    </tr>
                    <tr>
                        <td><xsl:attribute name="class">lside</xsl:attribute>Speed</td>
                        <td colspan="2"><xsl:attribute name="class">rside</xsl:attribute><xsl:value-of select="/flights/flight/route[routename/. = $code]/speed"/><xsl:value-of select="/flights/flight/route[routename/. = $code]/speed/@ratio"/></td>
                    </tr>




               <tr>
                    <td><xsl:attribute name="class">lside</xsl:attribute>Aircraft</td>
                     <td colspan="3">
                        <xsl:attribute name="class">lside</xsl:attribute>
                            <a><xsl:attribute name="href">aircraft.php?a=<xsl:value-of select="/flights/flight/plane[../route/routename/. =$code]/name" /></xsl:attribute>
                                <xsl:value-of select="/flights/flight/plane[../route/routename/. =$code]/name"/>
                            </a>
                    </td>
                </tr>

            </table>    
            <!-- End the map and route table -->

        </xsl:element>
        <!-- End the Body element -->

    </xsl:element>
    <!-- End the Html element -->

</xsl:template>
<!-- End the Main Template element -->

如何将持续时间从 PT12H37M 转换为 12 h 37 m,使其在屏幕上正常显示?

4

2 回答 2

2

首先,您的 XSLT 非常冗长。你知道吗?而不是这样写:

            <xsl:element name="a">
                <xsl:attribute name="id">
                    <xsl:text>navlinkstwo</xsl:text>
                </xsl:attribute>
                <xsl:attribute name="href">planes.php</xsl:attribute>
            </xsl:element>

你可以这样写:

<a id="navlinkstwo" href="planes.php"/>

其次,你还没有说它是如何失败的。Martin 猜测您可能没有使用 XSLT 2.0 处理器,但这似乎是纯粹的猜测,没有任何证据。如果你告诉我们它是如何失败的,我们就不必猜测了。

于 2013-02-19T14:03:19.527 回答
1

您想使用像 Saxon 9 或 AltovaXML 或 XmlPrime 这样的 XSLT 2.0 处理器吗?

在这种情况下,您可以使用

<xsl:template match="duration">
  <xsl:variable name="d" select="xs:dayTimeDuration(.)"/>
  <xsl:value-of select="concat(days-from-duration($d), ' h ', minutes-from-duration($d), ' m')"/>
</xsl:template>

请参阅http://www.w3.org/TR/xpath-functions/#func-minutes-from-duration

如果您想坚持使用单个模板样式表,请替换

                    <tr>
<!-- THIS IS WHERE I OUTPUT THE DURATION AND I THINK THIS IS WHERE I NEED TO TRANSFORM IT -->
                        <td><xsl:attribute name="class">lside</xsl:attribute>Duration</td>
                        <td colspan="2"><xsl:attribute name="class">rside</xsl:attribute><xsl:value-of select="/flights/flight/route[routename/. = $code]/duration"/></td>
                    </tr>

                    <tr>
<!-- THIS IS WHERE I OUTPUT THE DURATION AND I THINK THIS IS WHERE I NEED TO TRANSFORM IT -->
                        <td class="lside">Duration</td>
                        <xsl:variable name="d" select="xs:dayTimeDuration(/flights/flight/route[routename/. = $code]/duration)" xmlns:xs="http://www.w3.org/2001/XMLSchema"/>
                        <td colspan="2" class="rside"><xsl:value-of select="concat(days-from-duration($d), ' h ', minutes-from-duration($d), ' m')"/></td>
                    </tr>
于 2013-02-19T11:06:36.580 回答