需要使用xslt仅显示此xml中的少数属性

时间:2013-03-03 16:54:59

标签: xml xslt

Asalmualikum,兄弟我需要使用xslt显示来自webservice的xml,我还需要使用xslt文件显示xml中的一些属性。这是我的样本xml

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="XSLTFile.xsl"?>
 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
     <ns1:OTA_AirLowFareSearchRS Version="2.001" xmlns="http://www.opentravel.org/OTA/2003/05" xmlns:ns1="http://www.opentravel.org/OTA/2003/05">
      <ns1:Success />
       <ns1:PricedItineraries>
        <ns1:PricedItinerary CompositeFlightNumber="AI" CountCompositeFlightNumber="1" FareType="Non-Refundable" InboundSegmentReference="1" MatrixFare="true" Mode="" OriginDestinationRPH="BLRHYDAI517AI20130321" OutboundSegmentReference="1" Priority="1" RecommendationRPH="1" Refundable="true" ReturnOnly="false" SequenceNumber="1.0" SupplierCode="1AWS">
           <ns1:AirItinerary SupplierSystem="Amadeus">
             <ns1:OriginDestinationOptions>
               <ns1:OriginDestinationOption Duration="01:05:00" FlightID="BLRHYDAI517AI2013-03-21" MajorityCarrier="AI" ReturnOnly="false" SupplierCode="1AWS" SupplierSystem="Amadeus" UniqueIdentifier="1.0">
                 <ns1:FlightSegment ArrivalDateTime="2013-03-21T09:20:00" CabinCode="Y" DeliveryMethod="Courier" DepartureDateTime="2013-03-21T08:15:00" Duration="01:05:00" FlightNumber="517" LTD="1AWS" LineNumber="Y" NumberInParty="BLRHYD" RPH="1" ResBookDesigCode="E" TicketType="Physical" ValidConnectionInd="1AWS">
                  <ns1:DepartureAirport AirPortName="Bengaluru" CityName="Bangalore" LocationCode="BLR" />
                  <ns1:ArrivalAirport AirPortName="Shamshabad Rajiv Gandhi" CityName="Hyderabad" LocationCode="HYD" />
                  <ns1:OperatingAirline Code="AI" />
                  <ns1:BookingClassAvail FareType="RP" ResBookDesigCode="E" ResBookDesigQuantity="5" Status="5" WebFareName="EAP30" />
                  <ns1:Equipment AirEquipType="319" />
                  <ns1:MarketingAirline Code="AI" MatrixCode="AI" Name="Air India" YTAirlineCode="AI" />
                  <ns1:ValidatingCarrier Code="AI" />
                </ns1:FlightSegment>
                 <ns1:FormData>
                  <ns1:FBC Destination="HYD" FlightNumber="517" LineNumber="Y" Origin="BLR" SeatToSell="5" WebFareName="EAP30" />
                  <ns1:FareDifference>
                    <ns1:TotalFare ADT="1" BaseFare="670-ADT 670" CHD="0" Cabin="Economy" HostName="railserver" INF="0" Rbd="RP - E -" Tax="TTL-2355">3032</ns1:TotalFare>
                  </ns1:FareDifference>
                  <ns1:TicketingInfo DeliveryMethod="Courier" TicketTimeLimit="2013-03-03 18:43:00" TicketType="Physical" />
                   <ns1:AgentMarkup>
                     <ns1:Airlines>
                       <ns1:Airline>
                        <ns1:Code>6E</ns1:Code>
                        <ns1:GDS>0</ns1:GDS>
                        <ns1:WholeSalerFees>0.0000</ns1:WholeSalerFees>
                        <ns1:WholeSalerFeesType>F</ns1:WholeSalerFeesType>
                        <ns1:CommissionType>C</ns1:CommissionType>
                        <ns1:ApplyOn>BF</ns1:ApplyOn>
                        <ns1:Amount>0.0000</ns1:Amount>
                        <ns1:Type>P</ns1:Type>
                        <ns1:DiscountPLB>0.0000</ns1:DiscountPLB>
                        <ns1:DiscountPLBType>F</ns1:DiscountPLBType>
                        <ns1:DiscountPLBOn />
                        <ns1:Surcharge>2700.0000</ns1:Surcharge>
                      </ns1:Airline>
                       <ns1:Airline>
                        <ns1:Code>9H</ns1:Code>
                        <ns1:GDS>0</ns1:GDS>
                        <ns1:WholeSalerFees>0.0000</ns1:WholeSalerFees>
                        <ns1:WholeSalerFeesType>F</ns1:WholeSalerFeesType>
                        <ns1:CommissionType>M</ns1:CommissionType>
                        <ns1:ApplyOn>BF</ns1:ApplyOn>
                        <ns1:Amount>6.0000</ns1:Amount>
                        <ns1:Type>P</ns1:Type>
                        <ns1:DiscountPLB>0.0000</ns1:DiscountPLB>
                        <ns1:DiscountPLBType>F</ns1:DiscountPLBType>
                        <ns1:DiscountPLBOn />
                        <ns1:Surcharge>0.0000</ns1:Surcharge>
                      </ns1:Airline>
                       <ns1:Airline>
                        <ns1:Code>9W</ns1:Code>
                        <ns1:GDS>1</ns1:GDS>
                        <ns1:WholeSalerFees>0.0000</ns1:WholeSalerFees>
                        <ns1:WholeSalerFeesType>F</ns1:WholeSalerFeesType>
                        <ns1:CommissionType>C</ns1:CommissionType>
                        <ns1:ApplyOn>BF</ns1:ApplyOn>
                        <ns1:Amount>0.0000</ns1:Amount>
                        <ns1:Type>P</ns1:Type>
                        <ns1:DiscountPLB>0.0000</ns1:DiscountPLB>
                        <ns1:DiscountPLBType>F</ns1:DiscountPLBType>
                        <ns1:DiscountPLBOn />
                        <ns1:Surcharge>1100.0000</ns1:Surcharge>
                      </ns1:Airline>
                       <ns1:Airline>
                        <ns1:Code>AI</ns1:Code>
                        <ns1:GDS>1</ns1:GDS>
                        <ns1:WholeSalerFees>0.0000</ns1:WholeSalerFees>
                        <ns1:WholeSalerFeesType>F</ns1:WholeSalerFeesType>
                        <ns1:CommissionType>C</ns1:CommissionType>
                        <ns1:ApplyOn>BF</ns1:ApplyOn>
                        <ns1:Amount>0.0000</ns1:Amount>
                        <ns1:Type>P</ns1:Type>
                        <ns1:DiscountPLB>0.0000</ns1:DiscountPLB>
                        <ns1:DiscountPLBType>F</ns1:DiscountPLBType>
                        <ns1:DiscountPLBOn />
                        <ns1:Surcharge>1100.0000</ns1:Surcharge>
                      </ns1:Airline>
                       <ns1:Airline>
                        <ns1:Code>DN</ns1:Code>
                        <ns1:GDS>0</ns1:GDS>
                        <ns1:WholeSalerFees>0.0000</ns1:WholeSalerFees>
                        <ns1:WholeSalerFeesType>F</ns1:WholeSalerFeesType>
                        <ns1:CommissionType>M</ns1:CommissionType>
                        <ns1:ApplyOn>BF</ns1:ApplyOn>
                        <ns1:Amount>6.0000</ns1:Amount>
                        <ns1:Type>P</ns1:Type>
                        <ns1:DiscountPLB>0.0000</ns1:DiscountPLB>
                        <ns1:DiscountPLBType>F</ns1:DiscountPLBType>
                        <ns1:DiscountPLBOn />
                        <ns1:Surcharge>3250.0000</ns1:Surcharge>
                      </ns1:Airline>
                       <ns1:Airline>
                        <ns1:Code>G8</ns1:Code>
                        <ns1:GDS>0</ns1:GDS>
                        <ns1:WholeSalerFees>0.0000</ns1:WholeSalerFees>
                        <ns1:WholeSalerFeesType>F</ns1:WholeSalerFeesType>
                        <ns1:CommissionType>C</ns1:CommissionType>
                        <ns1:ApplyOn>BF</ns1:ApplyOn>
                        <ns1:Amount>0.0000</ns1:Amount>
                        <ns1:Type>P</ns1:Type>
                        <ns1:DiscountPLB>0.0000</ns1:DiscountPLB>
                        <ns1:DiscountPLBType>F</ns1:DiscountPLBType>
                        <ns1:DiscountPLBOn />
                        <ns1:Surcharge>2700.0000</ns1:Surcharge>
                      </ns1:Airline>
                       <ns1:Airline>
                        <ns1:Code>I7</ns1:Code>
                        <ns1:GDS>0</ns1:GDS>
                        <ns1:WholeSalerFees>0.0000</ns1:WholeSalerFees>
                        <ns1:WholeSalerFeesType>F</ns1:WholeSalerFeesType>
                        <ns1:CommissionType>M</ns1:CommissionType>
                        <ns1:ApplyOn>BF</ns1:ApplyOn>
                        <ns1:Amount>6.0000</ns1:Amount>
                        <ns1:Type>P</ns1:Type>
                        <ns1:DiscountPLB>0.0000</ns1:DiscountPLB>
                        <ns1:DiscountPLBType>F</ns1:DiscountPLBType>
                        <ns1:DiscountPLBOn />
                        <ns1:Surcharge>0.0000</ns1:Surcharge>
                      </ns1:Airline>
                       <ns1:Airline>
                        <ns1:Code>IC</ns1:Code>
                        <ns1:GDS>1</ns1:GDS>
                        <ns1:WholeSalerFees>0.0000</ns1:WholeSalerFees>
                        <ns1:WholeSalerFeesType>F</ns1:WholeSalerFeesType>
                        <ns1:CommissionType>C</ns1:CommissionType>
                        <ns1:ApplyOn>BF</ns1:ApplyOn>
                        <ns1:Amount>0.0000</ns1:Amount>
                        <ns1:Type>P</ns1:Type>
                        <ns1:DiscountPLB>0.0000</ns1:DiscountPLB>
                        <ns1:DiscountPLBType>F</ns1:DiscountPLBType>
                        <ns1:DiscountPLBOn />
                        <ns1:Surcharge>1100.0000</ns1:Surcharge>
                      </ns1:Airline>
                       <ns1:Airline>
                        <ns1:Code>IT</ns1:Code>
                        <ns1:GDS>1</ns1:GDS>
                        <ns1:WholeSalerFees>0.0000</ns1:WholeSalerFees>
                        <ns1:WholeSalerFeesType>F</ns1:WholeSalerFeesType>
                        <ns1:CommissionType>C</ns1:CommissionType>
                        <ns1:ApplyOn>BF</ns1:ApplyOn>
                        <ns1:Amount>0.0000</ns1:Amount>
                        <ns1:Type>P</ns1:Type>
                        <ns1:DiscountPLB>0.0000</ns1:DiscountPLB>
                        <ns1:DiscountPLBType>F</ns1:DiscountPLBType>
                        <ns1:DiscountPLBOn />
                        <ns1:Surcharge>1100.0000</ns1:Surcharge>
                      </ns1:Airline>
                       <ns1:Airline>
                        <ns1:Code>S2</ns1:Code>
                        <ns1:GDS>1</ns1:GDS>
                        <ns1:WholeSalerFees>0.0000</ns1:WholeSalerFees>
                        <ns1:WholeSalerFeesType>F</ns1:WholeSalerFeesType>
                        <ns1:CommissionType>C</ns1:CommissionType>
                        <ns1:ApplyOn>BF</ns1:ApplyOn>
                        <ns1:Amount>0.0000</ns1:Amount>
                        <ns1:Type>P</ns1:Type>
                        <ns1:DiscountPLB>0.0000</ns1:DiscountPLB>
                        <ns1:DiscountPLBType>F</ns1:DiscountPLBType>
                        <ns1:DiscountPLBOn />
                        <ns1:Surcharge>1100.0000</ns1:Surcharge>
                      </ns1:Airline>
                       <ns1:Airline>
                        <ns1:Code>SG</ns1:Code>
                        <ns1:GDS>0</ns1:GDS>
                        <ns1:WholeSalerFees>0.0000</ns1:WholeSalerFees>
                        <ns1:WholeSalerFeesType>F</ns1:WholeSalerFeesType>
                        <ns1:CommissionType>C</ns1:CommissionType>
                        <ns1:ApplyOn>BF</ns1:ApplyOn>
                        <ns1:Amount>0.0000</ns1:Amount>
                        <ns1:Type>P</ns1:Type>
                        <ns1:DiscountPLB>0.0000</ns1:DiscountPLB>
                        <ns1:DiscountPLBType>F</ns1:DiscountPLBType>
                        <ns1:DiscountPLBOn />
                        <ns1:Surcharge>2000.0000</ns1:Surcharge>
                      </ns1:Airline>
                       <ns1:Others>
                        <ns1:Code />
                        <ns1:GDS>1</ns1:GDS>
                        <ns1:WholeSalerFees>0</ns1:WholeSalerFees>
                        <ns1:CommissionType>C</ns1:CommissionType>
                        <ns1:ApplyOn>B</ns1:ApplyOn>
                        <ns1:Amount>0.0000</ns1:Amount>
                      </ns1:Others>
                    </ns1:Airlines>
                  </ns1:AgentMarkup>
                </ns1:FormData>
                <ns1:FormData />
              </ns1:OriginDestinationOption>
            </ns1:OriginDestinationOptions>
          </ns1:AirItinerary>
           <ns1:AirItineraryPricingInfo UniqueIdentifier="1.0">
             <ns1:ItinTotalFare NegotiatedFare="No">
              <ns1:BaseFare Amount="670" CurrencyCode="INR" />
               <ns1:Taxes>
                <ns1:Tax Amount="2355" CurrencyCode="INR" TaxCode="TTL" />
              </ns1:Taxes>
              <ns1:TotalFare Amount="3032" CurrencyCode="INR" />
               <ns1:Fees>
                <ns1:Fee Amount="0" />
                <ns1:Fee Amount="0" FeeCode="YatraLCCSurcharge" />
              </ns1:Fees>
              <ns1:ServiceTax Amount="7" Currency="INR" Description="Service Tax" ServiceTaxCode="ST" ServiceTaxRate="0.01" />
               <ns1:Markups>
                <ns1:Markup Amount="0" CurrencyCode="INR" Description="0.0 wholesalerfees for client 1152" MarkupCode="DisplayMarkup" MarkupType="W" Type="C" />
                <ns1:Markup Amount="0" CurrencyCode="INR" Description="0.0% on base fare for client - 1152" MarkupCode="DisplayMarkup" MarkupType="M" Type="C" />
                <ns1:Markup Amount="0" CurrencyCode="INR" Description="0.0% on fuel surcharge 0 -for client - 1152" MarkupCode="DisplayMarkup" MarkupType="M" Type="C" />
                <ns1:Markup Amount="0" CurrencyCode="INR" Description="0.0 on base fare for client - 1152" MarkupCode="DisplayMarkup" MarkupType="M" Type="P" />
              </ns1:Markups>
            </ns1:ItinTotalFare>
             <ns1:PTC_FareBreakdowns>
               <ns1:PTC_FareBreakdown>
                <ns1:PassengerTypeQuantity Code="ADT" Quantity="1.0" />
                 <ns1:FareBasisCodes>
                  <ns1:FareBasisCode FareType="" SegmentRPH="">EAP30</ns1:FareBasisCode>
                </ns1:FareBasisCodes>
                 <ns1:PassengerFare>
                  <ns1:BaseFare Amount="670" CurrencyCode="INR" />
                   <ns1:Taxes>
                    <ns1:Tax Amount="2355" CurrencyCode="INR" TaxCode="TTL" />
                  </ns1:Taxes>
                  <ns1:TotalFare Amount="3032" CurrencyCode="INR" />
                  <ns1:ServiceTax Amount="7" Currency="INR" Description="Service Tax" ServiceTaxCode="ST" ServiceTaxRate="0.01" />
                   <ns1:Markups>
                    <ns1:Markup Amount="0.0000" CurrencyCode="INR" Description="0.0 wholesalerfees for client 1152" MarkupCode="DisplayMarkup" MarkupType="W" Type="C" />
                    <ns1:Markup Amount="0" CurrencyCode="INR" Description="0.0 % on base fare for client - 1152" MarkupCode="DisplayMarkup" MarkupType="M" Type="C" />
                    <ns1:Markup Amount="0" Description="0.0% on fule surcharge 0.0for client1152" MarkupCode="DisplayMarkup" MarkupType="M" Type="C" />
                    <ns1:Markup Amount="0" CurrencyCode="INR" Description="0.0 on base fare for client1152" MarkupCode="DisplayMarkup" MarkupType="M" Type="P" />
                  </ns1:Markups>
                </ns1:PassengerFare>
              </ns1:PTC_FareBreakdown>
            </ns1:PTC_FareBreakdowns>
             <ns1:FareInfos>
               <ns1:FareInfo>
                <ns1:FareReference>EAP30</ns1:FareReference>
                <ns1:FilingAirline Code="ZZ" />
                <ns1:DepartureAirport LocationCode="BLR" />
                <ns1:ArrivalAirport LocationCode="HYD" />
              </ns1:FareInfo>
            </ns1:FareInfos>
          </ns1:AirItineraryPricingInfo>
          <ns1:TicketingInfo DeliveryMethod="Courier" TicketTimeLimit="2013-03-03 18:43:00" TicketType="Physical" />
          <ns1:Notes>PENALTY APPLIES</ns1:Notes>
          <ns1:Notes>LAST TKT DTE21OCT12 - SEE ADV PURCHASE</ns1:Notes>
        </ns1:PricedItinerary>
      </ns1:PricedItineraries >
    </ns1:OTA_AirLowFareSearchRS>
  </soapenv:Body >
</soapenv:Envelope >

以下是将xml文件显示为html

的xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="http://www.opentravel.org/OTA/2003/05"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>


  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="ns1:OTA_AirLowFareSearchRS">
    <table>
      <!--<tr>
        <th>
          Airline
        </th>
        <th>
          Departure
        </th>
        <th>

          Arrival
        </th>
        <th>
          Duration
        </th>
        <th>

          Fare Type
        </th>
        <th>
          Per Adult Fare
        </th>

      </tr>-->
      <xsl:for-each select ="ns1:OTA_AirLowFareSearchRS/ns1:PricedItineraries/ns1:PricedItinerary/ns1:AirItinerary/ns1:OriginDestinationOptions/ns1:OriginDestinationOption/ns1:FlightSegment/ns1:MarketingAirline">


        <tr>
          <td>
            <xsl:value-of select="Name"/>

          </td>


        </tr>

      </xsl:for-each>
    </table>
  </xsl:template>



</xsl:stylesheet>

我只显示一个用于测试目的的属性,你可以告诉我如何显示它。当我在浏览器中运行xml文件时它什么也没显示。

1 个答案:

答案 0 :(得分:0)

你的xsl创建一个空表,因为for-each XPath是错误的。将其更改为

<xsl:for-each select="ns1:PricedItineraries/ns1:PricedItinerary/ns1:AirItinerary/ns1:OriginDestinationOptions/ns1:OriginDestinationOption/ns1:FlightSegment/ns1:MarketingAirline">
   ...
   <xsl:value-of select="@Name" />
   ...
</xsl:for-each>

原因:

模板将匹配'ns1:OTA_AirLowFareSearchRS';该节点的有效子节点为“ns1:Success”和“ns1:PricedItineraries”,不是 OTA_AirLowFareSearchRS。此外,您还可以使用xsl:value-of。

中的@Name访问属性Name

更好的解决方案:

在'ns1:OTA_AirLowFareSearchRS'的匹配中创建表,然后调用apply-templates;然后使用for-each中的表达式进行另一个模板匹配。处理器将为您完成工作,并且很容易扩展功能。

<xsl:template match="ns1:OTA_AirLowFareSearchRS">
  <table>
    <xsl:apply-templates />
  </table>
</xsl:template>

<xsl:template match="ns1:PricedItineraries/ns1:PricedItinerary/ns1:AirItinerary/ns1:OriginDestinationOptions/ns1:OriginDestinationOption/ns1:FlightSegment/ns1:MarketingAirline">
  <tr><td>
    <xsl:value-of select="@Name"/>
  </td></tr>
</xsl:template>

<!-- other templates for other table rows -->
<xsl:template match="...">