2016-01-29 83 views
0

工作,下面是XML如何命名空間中的XML

<?xml version="1.0" encoding="UTF-8"?> 
<hotels> 
<hotel> 
    <rooms> 
     <room> 
      <rates> 
       <rate id="1" adults="1" child="0"></rate> 
       <rate id="2" adults="2" child="0"></rate> 
       <rate id="3" adults="1" child="0"></rate> 
      </rates> 
     </room> 
     <room> 
      <rates> 
       <rate id="4" adults="1" child="0"></rate> 
       <rate id="5" adults="2" child="0"></rate> 
       <rate id="6" adults="2" child="0"></rate> 
      </rates> 
     </room> 
    </rooms> 
</hotel> 
</hotels> 

我試試下面的XSLT。 XSLT工作正常。我得到的結果按照我需要

<?xml version="1.0" encoding="ISO-8859-1"?> 
     <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 

      <xsl:key name="by-occupancy" match="rooms/room/rates/rate" use="concat(generate-id(parent::rooms),\'|\',@adults)"/> 

      <xsl:template match="hotels/hotel"> 
       <hotel> 
       <xsl:apply-templates select="rooms/room/rates/rate[generate-id() = generate-id(key(\'by-occupancy\', concat(generate-id(parent::rooms),\'|\',@adults))[1])]" mode="fun_options"/> 
       </hotel> 
      </xsl:template> 

      <xsl:template match="rate" mode="fun_options"> 
      <rates> 
       <xsl:for-each select="key(\'by-occupancy\', concat(generate-id(parent::rooms),\'|\',@adults))"> 
       <rate><xsl:value-of select="@id"/>-<xsl:value-of select="@adults"/></rate> 
       </xsl:for-each> 
      </rates> 
      </xsl:template> 

     </xsl:stylesheet> 

但是,當我applay的xmlns:在XML XSI。

結果獲得空白

<?xml version="1.0" encoding="UTF-8"?> 
<hotels xmlns="http://www.test.com/schemas/messages" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://www.test.com/schemas/messages"> 
<hotel > 
    <rooms> 
     <room> 
      <rates> 
       <rate id="1" adults="1" child="0"></rate> 
       <rate id="2" adults="2" child="0"></rate> 
       <rate id="3" adults="1" child="0"></rate> 
      </rates> 
     </room> 
     <room> 
      <rates> 
       <rate id="4" adults="1" child="0"></rate> 
       <rate id="5" adults="2" child="0"></rate> 
       <rate id="6" adults="2" child="0"></rate> 
      </rates> 
     </room> 
    </rooms> 
</hotel> 
</hotels> 

下面XSLT我嘗試。在這裏,我applay排除對結果的前綴=「MS XSI」

<?xml version="1.0" encoding="ISO-8859-1"?> 
     <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ms="http://www.test.com/schemas/messages" exclude-result-prefixes="ms xsi"> 
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 

      <xsl:key name="by-occupancy" match="ms:rooms/ms:room/ms:rates/ms:rate" use="concat(generate-id(parent::ms:rooms),\'|\',@adults)"/> 

      <xsl:template match="ms:hotels/ms:hotel"> 
       <hotel> 
       <xsl:apply-templates select="ms:rooms/ms:room/ms:rates/ms:rate[generate-id() = generate-id(key(\'by-occupancy\', concat(generate-id(parent::ms:rooms),\'|\',@adults))[1])]" mode="fun_options"/> 
       </hotel> 
      </xsl:template> 

      <xsl:template match="ms:rate" mode="fun_options"> 
      <rates> 
       <xsl:for-each select="key(\'by-occupancy\', concat(generate-id(parent::ms:rooms),\'|\',@adults))"> 
       <rate><xsl:value-of select="@id"/>-<xsl:value-of select="@adults"/></rate> 
       </xsl:for-each> 
      </rates> 
      </xsl:template> 

     </xsl:stylesheet> 

我需要導致類似下面

<hotel> 
    <rates> 
    <rate>1-1</rate> 
    <rate>3-1</rate> 
    <rate>4-1</rate> 
    </rates> 
    <rates> 
    <rate>2-2</rate> 
    <rate>5-2</rate> 
    <rate>6-2</rate> 
    </rates> 
</hotel> 

回答

0

我看不出任何的轉義字符串會編的。並且你正試圖在密鑰中使用的父母不存在,所以我認爲你想要一個祖先:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ms="http://www.test.com/schemas/messages" exclude-result-prefixes="ms xsi"> 
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 

      <xsl:key name="by-occupancy" match="ms:rooms/ms:room/ms:rates/ms:rate" use="concat(generate-id(ancestor::ms:rooms),'|',@adults)"/> 

      <xsl:template match="ms:hotels/ms:hotel"> 
       <hotel> 
       <xsl:apply-templates select="ms:rooms/ms:room/ms:rates/ms:rate[generate-id() = generate-id(key('by-occupancy', concat(generate-id(ancestor::ms:rooms),'|',@adults))[1])]" mode="fun_options"/> 
       </hotel> 
      </xsl:template> 

      <xsl:template match="ms:rate" mode="fun_options"> 
      <rates> 
       <xsl:for-each select="key('by-occupancy', concat(generate-id(ancestor::ms:rooms),'|',@adults))"> 
       <rate><xsl:value-of select="@id"/>-<xsl:value-of select="@adults"/></rate> 
       </xsl:for-each> 
      </rates> 
      </xsl:template> 

     </xsl:stylesheet> 
+0

真棒....謝謝 – Note