2012-02-18 47 views
1

我对名称空间的xsl没有太多经验,我使用xsl来转换xml。XSL无法正常工作的XML名称空间

这里是XML输入,

<GetAvailability> 
    <RequestSection> 
     <Hotel> 
      <StayDateRange Start="25/02/2012" End="27/02/2012"/> 
      <HotelSearchCriteria> 
       <HotelRef HotelCityName="MUMBAI" HotelCityCode="666" Currency="USD" Nationality="US"/> 
       <Rooms> 
       <Room Type="Single" ChildCount="1" AdultsCount="1" ExtraBed="0" RateBasis="-1"> 
        <Ages> 
         <Age>5</Age> 
        </Ages> 
       </Room> 
      </Rooms> 

      </HotelSearchCriteria> 
     </Hotel> 
    </RequestSection> 
</GetAvailability> 

我期待的输出,

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:urn="urn:b2bHotelSOAP" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"> 
    <soap:Header/> 
    <soap:Body> 
     <urn:getAvailableHotel soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding"> 
      <destinationId xsi:type="xsd:string">666</destinationId> 
      <checkIn xsi:type="xsd:date">2011-09-01</checkIn> 
      <checkOut xsi:type="xsd:date">2011-09-03</checkOut> 
      <currency xsi:type="xsd:string">USD</currency> 
      <clientNationality xsi:type="xsd:string">US</clientNationality> 
      <onRequest xsi:type="xsd:boolean">True</onRequest> 
         <rooms xsi:type="urn:roomArray" soapenc:arrayType="urn:paxesArray"> 
      <paxes> 
       <pax> 
        <paxType>Adult</paxType> 
       </pax> 
      </paxes> 
         </rooms> 

     </urn:getAvailableHotel> 
    </soap:Body> 
</soap:Envelope> 

编辑:这里是xsl我都试过了,

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
    <xsl:template name="start" match="/"> 
     <xsl:if test="//HotelSearchCriteria/HotelRef/@HotelCityName != ''"> 
      <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:urn="urn:b2bHotelSOAP" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"> 
       <soap:Header/> 
       <soap:Body> 
        <urn:getAvailableHotel soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding"> 
         <apiKey xsi:type="xsd:string"> 
          <xsl:value-of select="//ApiKey"/> 
         </apiKey> 
         <destinationId xsi:type="xsd:string"> 
          <xsl:value-of select="//HotelRef/@HotelCityCode"/> 
         </destinationId> 
         <checkIn xsi:type="xsd:date"> 
          <xsl:value-of select="//StayDateRange/@Start"/> 
         </checkIn> 
         <checkOut xsi:type="xsd:date"> 
          <xsl:value-of select="//StayDateRange/@End"/> 
         </checkOut> 
         <currency xsi:type="xsd:string"> 
          <xsl:value-of select="//Currency"/> 
         </currency> 
         <clientNationality xsi:type="xsd:string"> 
          <xsl:value-of select="//Nationality"/> 
         </clientNationality> 
         <onRequest xsi:type="xsd:boolean"> 
          <xsl:value-of select="//HotelAdvanacedSearchCriteria/Available"/> 
         </onRequest> 
         <rooms xsi:type="urn:roomArray" soapenc:arrayType="urn:paxesArray"> 
          <xsl:apply-templates select="//HotelSearchCriteria/Rooms/Room" mode="search"/> 

         </rooms> 
         <filters xsi:type="urn:filterArray" soapenc:arrayType="urn:filter"/> 
        </urn:getAvailableHotel> 
       </soap:Body> 
      </soap:Envelope> 
     </xsl:if> 
    </xsl:template> 
    <xsl:template match="Room"> 
     <paxes> 
      <xsl:attribute name="xsi:type">urn:paxesArray</xsl:attribute> 

     </paxes> 
    </xsl:template> 

</xsl:stylesheet> 

请向我建议此查询的解决方案。

+1

[你有什么尝试?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – nfechner 2012-02-18 08:59:40

+0

@nfechner非常好! – Filburt 2012-02-18 09:08:52

+0

用xsl编辑我已经尝试过了! – Sujit 2012-02-18 09:09:32

回答

1

好像你的实际问题是在Room模板...

<xsl:template match="Room"> 
    <paxes> 
     <xsl:attribute name="xsi:type">urn:paxesArray</xsl:attribute> 
    </paxes> 
</xsl:template> 

...因为你的XSL会抱怨它不承认xsi命名空间。

要解决这个问题,您必须将名称空间添加到您打算使用它的所有模板 - 或将其添加到顶层<xsl:stylesheet />之内。

<xsl:template match="Room" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <paxes> 
     <xsl:attribute name="xsi:type">urn:paxesArray</xsl:attribute> 
    </paxes> 
</xsl:template> 

或 - 如果你想保持你在start模板

<xsl:template match="Room" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <paxes xsi:type="urn:paxesArray"> 
     <xsl:apply-templates /> 
    </paxes> 
</xsl:template> 

添加您的属性的方式,你必须改变应用您Room模板中rooms只是<apply-templates />

<rooms xsi:type="urn:roomArray" soapenc:arrayType="urn:paxesArray"> 
    <xsl:apply-templates /> 
</rooms> 
+0

感谢您的解决方案,它工作正常。 – Sujit 2012-02-18 13:11:16