2013-03-24 15 views
1

在Firefox上,我收到加載樣式表時出錯:處理XPath表達式失敗。但在Chrome中沒有顯示。爲什麼我得到錯誤加載樣式表:XPath expr。失敗(XSD和XSL文件出現XML錯誤)?

objectshop.xml

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="objectshop.xsl" ?> 

<os:objectshop xmlns:os="http://www.example.org/objectshop" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.example.org/objectshop objectshop.xsd "> 
    <os:catalog> 
    <os:product> 
     <os:productId>1</os:productId> 
     <os:name>Achtung Baby - U2</os:name> 
     <os:description>Audio CD</os:description> 
     <os:price>21.0</os:price> 
     <os:campaignPrice>19.0</os:campaignPrice> 
     <os:icon>images/u2AchtungBaby.jpg</os:icon> 
    </os:product> 
    <os:product> 
     <os:productId>2</os:productId> 
     <os:name>Achtung Baby - U2</os:name> 
     <os:description>Audio CD</os:description> 
     <os:price>21.0</os:price> 
     <os:campaignPrice>19.0</os:campaignPrice> 
     <os:icon>images/u2AchtungBaby.jpg</os:icon> 
    </os:product> 
    <os:product> 
     <os:productId>2</os:productId> 
     <os:name>Achtung Baby - U2</os:name> 
     <os:description>Audio CD</os:description> 
     <os:price>21.0</os:price> 
     <os:campaignPrice>19.0</os:campaignPrice> 
     <os:icon>images/u2AchtungBaby.jpg</os:icon> 
    </os:product> 
    <os:product> 
     <os:productId>4</os:productId> 
     <os:name>Achtung Baby - U2</os:name> 
     <os:description>Audio CD</os:description> 
     <os:price>21.0</os:price> 
     <os:campaignPrice>19.0</os:campaignPrice> 
     <os:icon>images/u2AchtungBaby.jpg</os:icon> 
    </os:product> 
    </os:catalog> 
    <os:members> 
    <os:customer> 
     <os:userId>eteration</os:userId> 
     <os:name>Esma Meral</os:name> 
     <os:address>ITU andesea</os:address> 
     <os:phone>+90 123 456 789</os:phone> 
     <os:email>[email protected]</os:email> 
    </os:customer> 
    </os:members> 
</os:objectshop> 

objectshop.xsl

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.1" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
    <html> 
     <body> 
     <div id="headerregion"> 
     </div> 

     <div id="middleregion"> 
      <div id="middle"> 
      <div id="sidebar"> 
       ... 
      </div> 
      <div id="content"> 
       <xsl:apply-templates select="/os:objectshop/os:catalog" /> 
      </div> 
      </div> 
     </div> 
     <div id="footerregion"> 
      Something 
     </div> 
     </body> 
    </html> 
    </xsl:template> 
    <xsl:template match="/os:objectshop/os:catalog"> 
    <table align="center"> 
     <xsl:for-each select="//os:product"> 
     <xsl:sort select="os:productId" /> 
     <tr valign="top"> 
      <td valign="top"> 
      <b> 
       <xsl:value-of select="os:productId" /> 
      </b> 
      </td> 
      <td> 
      <img> 
       <xsl:attribute name="height">50</xsl:attribute> 
       <xsl:attribute name="width">50</xsl:attribute> 
       <xsl:attribute name="align">top</xsl:attribute> 
       <xsl:attribute name="border">0</xsl:attribute> 
       <xsl:attribute name="src"> 
        <xsl:value-of select="os:icon" /> 
       </xsl:attribute> 
       xsl:attribute> 
      </img> 
      </td> 
      <td> 
      <b> 
       <xsl:value-of select="os:name" /> 
      </b> 
      <xsl:value-of select="os:description" /> 
      <br /> 
      Retail Price $ 
      <xsl:value-of select="os:price" /> 
      <br /> 
      <font color="RED"> 
       <b> 
       Our Price $ 
       <xsl:value-of select="os:price" /> 
       </b> 
      </font> 
      </td> 
     </tr> 
     <tr> 
      <td colspan="3"> 
      <img height="1" src="images/grey-pixel.gif" width="100%" 
       align="top" vspace="6" /> 
      </td> 
     </tr> 
     </xsl:for-each> 
    </table> 
    </xsl:template> 
</xsl:stylesheet> 

objectshop.xsd

<?xml version="1.0" encoding="UTF-8"?> 
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
targetNamespace="http://www.example.org/objectshop" 
elementFormDefault="qualified" 
xmlns:os="http://www.example.org/objectshop"> 

    <complexType name="ProductType"> 
     <sequence> 
      <element name="productId" type="string"></element> 
      <element name="name" type="string"></element> 
      <element name="description" type="string"></element> 
      <element name="price" type="double"></element> 
      <element name="campaignPrice" type="double"></element> 
      <element name="icon" type="string"></element> 
     </sequence> 
    </complexType> 

    <complexType name="CustomerType"> 
     <sequence> 
      <element name="userId" type="string"></element> 
      <element name="name" type="string"></element> 
      <element name="address" type="string"></element> 
      <element name="phone" type="string"></element> 
      <element name="email" type="string"></element> 
     </sequence> 
    </complexType> 

    <complexType name="CatalogType"> 
     <sequence> 
      <element name="product" type="os:ProductType" 
       minOccurs="0" maxOccurs="unbounded"> 
      </element> 
     </sequence> 
    </complexType> 

    <complexType name="MembersType"> 
     <sequence> 
      <element name="customer" type="os:CustomerType" minOccurs="0" maxOccurs="unbounded"></element> 
     </sequence> 
    </complexType> 

    <element name="objectshop" type="os:ObjectShopType"></element> 

    <complexType name="ObjectShopType"> 
     <sequence> 
      <element name="catalog" type="os:CatalogType"></element> 
      <element name="members" type="os:MembersType"></element> 
     </sequence> 
    </complexType> 
</schema> 

回答

3

命名空間PREF XPath表達式中的ixes引用樣式表中聲明的名稱空間,而不是源文檔。您在未聲明的xpath中使用前綴os:,以修復將objectshop.xml中的xmlns:os複製到objectshop.xsl中的xsl:stylesheet標記所需的錯誤。

請注意,重要的是命名空間URI,而不是前綴。您可以在樣式表中聲明xmlns:s="http://www.example.org/objectshop",然後在xpath中使用/s:product,並且它仍然會與源文檔中的<os:product>匹配,因爲名稱空間是正確的。

+0

是的,它修正了錯誤但仍不顯示內容? Joe 2013-03-24 10:30:22

+0

@Joe樣式表對我來說看起來很明智,除了兩件事情 - 'version =「1.1」'應該是'1.0', for-each select中的'// os:product'應該是'os:product'。 – 2013-03-24 10:36:44

+0

我仍然有同樣的結果:除了「... Something」字符串 – Joe 2013-03-24 12:35:06

0

以下是更正代碼(添加的命名空間定義):

<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:os="http://www.example.org/objectshop"> 
    <xsl:template match="/"> 
    <html> 
     <body> 
     <div id="headerregion"> 
     </div> 

     <div id="middleregion"> 
      <div id="middle"> 
      <div id="sidebar"> 
       ... 
      </div> 
      <div id="content"> 
       <xsl:apply-templates select="/os:objectshop/os:catalog" /> 
      </div> 
      </div> 
     </div> 
     <div id="footerregion"> 
      Something 
     </div> 
     </body> 
    </html> 
    </xsl:template> 
    <xsl:template match="/os:objectshop/os:catalog"> 
    <table align="center"> 
     <xsl:for-each select="//os:product"> 
     <xsl:sort select="os:productId" /> 
     <tr valign="top"> 
      <td valign="top"> 
      <b> 
       <xsl:value-of select="os:productId" /> 
      </b> 
      </td> 
      <td> 
      <img> 
       <xsl:attribute name="height">50</xsl:attribute> 
       <xsl:attribute name="width">50</xsl:attribute> 
       <xsl:attribute name="align">top</xsl:attribute> 
       <xsl:attribute name="border">0</xsl:attribute> 
       <xsl:attribute name="src"> 
        <xsl:value-of select="os:icon" /> 
       </xsl:attribute> 
       xsl:attribute> 
      </img> 
      </td> 
      <td> 
      <b> 
       <xsl:value-of select="os:name" /> 
      </b> 
      <xsl:value-of select="os:description" /> 
      <br /> 
      Retail Price $ 
      <xsl:value-of select="os:price" /> 
      <br /> 
      <font color="RED"> 
       <b> 
       Our Price $ 
       <xsl:value-of select="os:price" /> 
       </b> 
      </font> 
      </td> 
     </tr> 
     <tr> 
      <td colspan="3"> 
      <img height="1" src="images/grey-pixel.gif" width="100%" 
       align="top" vspace="6" /> 
      </td> 
     </tr> 
     </xsl:for-each> 
    </table> 
    </xsl:template> 
</xsl:stylesheet> 

當這種變換所提供的XML文檔應用:

<os:objectshop xmlns:os="http://www.example.org/objectshop" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.example.org/objectshop objectshop.xsd "> 
    <os:catalog> 
     <os:product> 
      <os:productId>1</os:productId> 
      <os:name>Achtung Baby - U2</os:name> 
      <os:description>Audio CD</os:description> 
      <os:price>21.0</os:price> 
      <os:campaignPrice>19.0</os:campaignPrice> 
      <os:icon>images/u2AchtungBaby.jpg</os:icon> 
     </os:product> 
     <os:product> 
      <os:productId>2</os:productId> 
      <os:name>Achtung Baby - U2</os:name> 
      <os:description>Audio CD</os:description> 
      <os:price>21.0</os:price> 
      <os:campaignPrice>19.0</os:campaignPrice> 
      <os:icon>images/u2AchtungBaby.jpg</os:icon> 
     </os:product> 
     <os:product> 
      <os:productId>2</os:productId> 
      <os:name>Achtung Baby - U2</os:name> 
      <os:description>Audio CD</os:description> 
      <os:price>21.0</os:price> 
      <os:campaignPrice>19.0</os:campaignPrice> 
      <os:icon>images/u2AchtungBaby.jpg</os:icon> 
     </os:product> 
     <os:product> 
      <os:productId>4</os:productId> 
      <os:name>Achtung Baby - U2</os:name> 
      <os:description>Audio CD</os:description> 
      <os:price>21.0</os:price> 
      <os:campaignPrice>19.0</os:campaignPrice> 
      <os:icon>images/u2AchtungBaby.jpg</os:icon> 
     </os:product> 
    </os:catalog> 
    <os:members> 
     <os:customer> 
      <os:userId>eteration</os:userId> 
      <os:name>Esma Meral</os:name> 
      <os:address>ITU andesea</os:address> 
      <os:phone>+90 123 456 789</os:phone> 
      <os:email>[email protected]</os:email> 
     </os:customer> 
    </os:members> 
</os:objectshop> 

的(似乎什麼要)正確,想要的結果是:

<html xmlns:os="http://www.example.org/objectshop"> 
    <body> 
     <div id="headerregion"></div> 
     <div id="middleregion"> 
     <div id="middle"> 
      <div id="sidebar"> 
       ... 

      </div> 
      <div id="content"> 
       <table align="center"> 
        <tr valign="top"> 
        <td valign="top"><b>1</b></td> 
        <td><img height="50" width="50" align="top" border="0" src="images/u2AchtungBaby.jpg"> 
          xsl:attribute&gt; 
          </td> 
        <td><b>Achtung Baby - U2</b>Audio CD<br> 
         Retail Price $ 
         21.0<br><font color="RED"><b> 
           Our Price $ 
           21.0</b></font></td> 
        </tr> 
        <tr> 
        <td colspan="3"><img height="1" src="images/grey-pixel.gif" width="100%" align="top" vspace="6"></td> 
        </tr> 
        <tr valign="top"> 
        <td valign="top"><b>2</b></td> 
        <td><img height="50" width="50" align="top" border="0" src="images/u2AchtungBaby.jpg"> 
          xsl:attribute&gt; 
          </td> 
        <td><b>Achtung Baby - U2</b>Audio CD<br> 
         Retail Price $ 
         21.0<br><font color="RED"><b> 
           Our Price $ 
           21.0</b></font></td> 
        </tr> 
        <tr> 
        <td colspan="3"><img height="1" src="images/grey-pixel.gif" width="100%" align="top" vspace="6"></td> 
        </tr> 
        <tr valign="top"> 
        <td valign="top"><b>2</b></td> 
        <td><img height="50" width="50" align="top" border="0" src="images/u2AchtungBaby.jpg"> 
          xsl:attribute&gt; 
          </td> 
        <td><b>Achtung Baby - U2</b>Audio CD<br> 
         Retail Price $ 
         21.0<br><font color="RED"><b> 
           Our Price $ 
           21.0</b></font></td> 
        </tr> 
        <tr> 
        <td colspan="3"><img height="1" src="images/grey-pixel.gif" width="100%" align="top" vspace="6"></td> 
        </tr> 
        <tr valign="top"> 
        <td valign="top"><b>4</b></td> 
        <td><img height="50" width="50" align="top" border="0" src="images/u2AchtungBaby.jpg"> 
          xsl:attribute&gt; 
          </td> 
        <td><b>Achtung Baby - U2</b>Audio CD<br> 
         Retail Price $ 
         21.0<br><font color="RED"><b> 
           Our Price $ 
           21.0</b></font></td> 
        </tr> 
        <tr> 
        <td colspan="3"><img height="1" src="images/grey-pixel.gif" width="100%" align="top" vspace="6"></td> 
        </tr> 
       </table> 
      </div> 
     </div> 
     </div> 
     <div id="footerregion"> 
     Something 

     </div> 
    </body> 
</html> 
相關問題