Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用XSLT在本地将XML链接到HTML页面_Html_Xml_Xslt - Fatal编程技术网

使用XSLT在本地将XML链接到HTML页面

使用XSLT在本地将XML链接到HTML页面,html,xml,xslt,Html,Xml,Xslt,我试图遵循forxslt的基本示例 xml.xml: <?xml version="1.0" encoding="UTF-8"?> <catalog> <cd> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <country>UK</country>

我试图遵循forxslt的基本示例

xml.xml:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <year>1982</year>
    </cd>
</catalog>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
  <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th style="text-align:left">Title</th>
        <th style="text-align:left">Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

隐藏你的心
邦尼泰勒
英国
哥伦比亚唱片公司
9.90
1988
最成功的
多莉·帕顿
美国
RCA
9.90
1982
xslt.xslt:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <year>1982</year>
    </cd>
</catalog>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
  <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th style="text-align:left">Title</th>
        <th style="text-align:left">Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

我的CD收藏
标题
艺术家
每当我尝试在W3学校以外的地方进行此操作时,它都不起作用,会出现以下错误

此XML文件似乎没有任何与之关联的样式信息。文档树如下所示

环顾堆栈溢出,我似乎找不到特定查询的答案,因为这应该是非常简单的

我的问题是:xslt文件如何知道我希望它使用xml.xml作为匹配的xml

从逻辑上讲,您可能会认为必须将其包含在xslt文件中。当我环顾四周时,我看到人们做了以下事情:

xml.xml:

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

xsl.xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="1.0">


但是这在本地对我不起作用,有什么建议吗?

XSLT文件不知道您想要使用
xml.xml
。相反,XML文件知道您想要使用
xslt.xslt

将此行作为第二行添加到
xml.xml
文件:

<?xml-stylesheet href="xslt.xslt" type="text/xsl" ?>


另外,请注意,Chrome不喜欢将XSL与
文件://
样式的URL一起使用。使用非Chrome浏览器或使用
http://
URL。

您是在Chrome浏览器中尝试此功能吗?确切地说,“不适合我”是什么意思?你做什么来测试它?你看到了什么结果?另外,在最后一个例子中,如果你提供的文件名是实际的,那么这行应该是:
。是的,你是对的,基本上我上面的工作,只是不在chrome中!