我正在尝试创建XML和XSL,但它给我一个错误,我不知道该怎么办

时间:2018-12-07 03:28:05

标签: javascript xml xslt

我试图创建一个链接到xsl的xml文件,每当我运行它时,我使用的任何浏览器都会出现相同的错误。我已经尝试研究该怎么做,但仍然不了解。我看到了一些有关安全性和权限的信息,但是我尝试了一下,但仍然得到了相同的结果。

有人可以帮忙吗?我是XML的新手,可以通过一本书学习HTML。我找不到任何对我有帮助的东西,并且真的想看看我做错了什么,或者此时的解决方法是什么。先感谢您!!

XML:

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

    <!DOCTYPE myProducts [
    <!ELEMENT myProducts (product)>
    <!ELEMENT product (prodName, prodImg, description, price, stockLevel, sku)>
    ]>

    <myProducts>
    <product id = "i7-8">
        <prodName>I7 8th Generation</prodName>
        <prodImg>/images.i7-8thgen.jpg</prodImg>
        <description>Core i7-8700K can reach a frequency of 4.7GHz. These chips 
    can be expanded with up to 40 platform PCIe 3.0 lanes.</description>
        <price>$300.00</price>
        <stockLevel>25</stockLevel>
        <sku>INT-78</sku>
    </product>

    <product id = "i5-8">
        <prodName>I5 8th Generation</prodName>
        <prodImg>/images.i5-8thgen.jpg</prodImg>
        <description>Intel Core i5-8400 comes with 6 processing Cores and 6 
    Threads.</description>
        <price>$250.00</price>
        <stockLevel>20</stockLevel>
        <sku>INT-58</sku>
    </product>

    <product id = "i3-8">
        <prodName>I3 8th Generation</prodName>
        <prodImg>/images.i3-8thgen.jpg</prodImg>
        <description>Intel Core i3 comes with 4 processing Cores and 4 Threads. 
   </description>
        <price>$200.00</price>
        <stockLevel>20</stockLevel>
        <sku>INT-38</sku>
    </product>

    <product id = "i5-7">
        <prodName>I5 7th Generation</prodName>
        <prodImg>/images.i5-7thgen.jpg</prodImg>
        <description>64-bit dual-core mid-range performance x86 mobile 
    microprocessor introduced by Intel in mid-2016.</description>
        <price>$170.00</price>
        <stockLevel>20</stockLevel>
        <sku>INT-57</sku>
    </product>

    <product id = "i3-7">
        <prodName>I3 7th Generation</prodName>
        <prodImg>/images.i3-7thgen.jpg</prodImg>
        <description>Core i3-7100U is a 64-bit dual-core low-end performance x86 
    mobile microprocessor introduced by Intel in mid-2016.</description>
        <price>$170.00</price>
        <stockLevel>20</stockLevel>
        <sku>INT-37</sku>
    </product>

    </myProducts>

到目前为止,我的XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:template match="/">
    <html>
      <head>
        <title>Chapter 10 XML</title>
      </head>
      <body>
        <div class="container">
          <xsl:for-each select="/chapter10XML/myProducts/product">
            <div class="product">
              <h3>
                <xsl:value-of select="@id"/>
              </h3>
              <p>
                <xsl:value-of select="price"/>
              </p>
              <p>
                <img src="[prodImg]"/>
              </p>
              <p>
                <xsl:value-of select="description"/>
              </p>
              <p>In Stock: <xsl.value-of select="stocklevel"/></p>
              <p>
                <xsl:value-of select="sku"/>
              </p>
            </div>
          </xsl:for-each>
        </div>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

基本上,您只需要更改两件事:

  • <xsl:output...中的输出方法更改为<xsl:output method="html" version="1.0" ...
  • 并将<xsl:for-each...更改为

    <xsl:for-each select="/myProducts/product">
    

    因为XML中不存在/chapter10XML根元素。

现在,您将获得可以在浏览器中呈现的HTML输出。


如果遇到安全错误,则可能是使用Chrome作为浏览器,它对执行本地文件有严格的限制。