使用XSD持续时间类型,使用XSLT将值输出到正常持续时间格式

问题描述 投票:0回答:2

我在我的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小时37米?

xml xslt xsd
2个回答
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>

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处理器,但这似乎是基于没有证据的纯猜测。如果你告诉我们它是如何失败的,我们就不用猜了。

© www.soinside.com 2019 - 2024. All rights reserved.