Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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
有人能告诉我将XML转换成HTML表的XSL吗_Html_Xml_Xslt_Xhtml_Html Table - Fatal编程技术网

有人能告诉我将XML转换成HTML表的XSL吗

有人能告诉我将XML转换成HTML表的XSL吗,html,xml,xslt,xhtml,html-table,Html,Xml,Xslt,Xhtml,Html Table,我被要求将一个包含内部DTD和外部XSL的XML文档转换成一个包含两组标准的整洁的表格——我选择了两个不同的作者 TBH我甚至不知道我是否有XML和DTD文档,对吗?当谈到使用XPath和XSLT时,我完全不知道该怎么做。 我希望有人能给我看一下完成的桌子,这样我就可以从中倒转过来,理解你是怎么做到的 这是我的XML文档 <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="B

我被要求将一个包含内部DTD和外部XSL的XML文档转换成一个包含两组标准的整洁的表格——我选择了两个不同的作者

TBH我甚至不知道我是否有XML和DTD文档,对吗?当谈到使用XPath和XSLT时,我完全不知道该怎么做。 我希望有人能给我看一下完成的桌子,这样我就可以从中倒转过来,理解你是怎么做到的

这是我的XML文档

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Batman.xsl"?>
<!DOCTYPE comics [
<!ELEMENT comics (name,author,publisher,country,year,prics)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT publisher (#PCDATA)>
<!ELEMENT country (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>
<comics>
   <batman>
       <issue2>
           <name>Batman Eternal</name>
           <author>Scott Synder</author>
           <publisher>DC Comics</publisher>
           <country>USA</country>
           <year>2012</year>
           <price>$2.99</price>
       </issue2>
       <issue3>
           <name>Batman Eternal</name>
           <author>Scott Synder</author>
           <publisher>DC Comics</publisher>
           <country>USA</country>
           <year>2012</year>
           <price>$2.99</price>
       </issue3>
       <issue4>
           <name>Batman Eternal</name>
           <author>Scott Synder</author>
           <publisher>DC Comics</publisher>
           <country>USA</country>
           <year>2012</year>
           <price>$2.99</price>
       </issue4>
       <issue5>
           <name>Batman Eternal</name>
           <author>Scott Synder</author>
           <publisher>DC Comics</publisher>
           <country>USA</country>
           <year>2012</year>
           <price>$2.99</price>
       </issue5>
       <issue6>
           <name>Batman Eternal</name>
           <author>Darren Darcer</author>
           <publisher>DC Comics</publisher>
           <country>USA</country>
           <year>2012</year>
           <price>$2.99</price>
       </issue6>
       <issue7>
           <name>Batman Eternal</name>
           <author>Darren Darcer</author>
           <publisher>DC Comics</publisher>
           <country>USA</country>
           <year>2012</year>
           <price>$2.99</price>
       </issue7>
       <issue8>
           <name>Batman Eternal</name>
           <author>Darren Darcer</author>
           <publisher>DC Comics</publisher>
           <country>USA</country>
           <year>2012</year>
           <price>$2.99</price>
       </issue8>
       <issue9>
           <name>Batman Eternal</name>
           <author>Darren Darcer</author>
           <publisher>DC Comics</publisher>
           <country>USA</country>
           <year>2012</year>
           <price>$2.99</price>
       </issue9>
    </batman>
</comics>

]>
永恒蝙蝠侠
斯科特·辛德尔
DC漫画
美国
2012
$2.99
永恒蝙蝠侠
斯科特·辛德尔
DC漫画
美国
2012
$2.99
永恒蝙蝠侠
斯科特·辛德尔
DC漫画
美国
2012
$2.99
永恒蝙蝠侠
斯科特·辛德尔
DC漫画
美国
2012
$2.99
永恒蝙蝠侠
达伦·达瑟
DC漫画
美国
2012
$2.99
永恒蝙蝠侠
达伦·达瑟
DC漫画
美国
2012
$2.99
永恒蝙蝠侠
达伦·达瑟
DC漫画
美国
2012
$2.99
永恒蝙蝠侠
达伦·达瑟
DC漫画
美国
2012
$2.99
@托马拉克-正如你所看到的,我不明白

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:template match="batman.xsl">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<table border="1">
      <th><td>Name</td><td>Author</td><td>Publisher</td><td>Country</td><td>Year</td><td>Price</td></th>
      <tr bgcolor="#ebebeb">
      <th>name</th>
      <th>author</th>
      <th>publisher</th>
      <th>country</th>
      <th>year</th>
      <th>price</th>
    </tr>
    <xsl:for-each select="comics/batman/issue">
    <tr>
      <td><xsl:value-of select="name"/></td>
      <td><xsl:value-of select="author"/></td>
      <td><xsl:value-of select="publisher"/></td>
      <td><xsl:value-of select="country"/></td>
      <td><xsl:value-of select="year"/></td>
      <td><xsl:value-of select="price"/></td>
    </tr>
    </xsl:for-each>
  </table>
</body>
</html>

</xsl:template>
</xsl:stylesheet>

NameAuthorPublisherCountryYearPrice
名称
作者
出版商
国家
年
价格

您的XML文本对于指定的DTD无效

如果指定的DTD错误且XML文本正确,则此XSL将生成HTML表:

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes" version="5.0"/>
  <xsl:template match="comics">
    <html>
      <head><link href="styles.css" rel="stylesheet" type="text/css"/></head>
      <body><xsl:apply-templates select="batman"/></body>
    </html>
  </xsl:template>
  <xsl:template match="batman">
    <table>
      <th><td>Name</td><td>Author</td><td>Publisher</td><td>Country</td><td>Year</td><td>Price</td></th>
      <xsl:for-each select="*">
        <tr>
          <td><xsl:value-of select="name"/></td>
          <td><xsl:value-of select="author"/></td>
          <td><xsl:value-of select="publisher"/></td>
          <td><xsl:value-of select="country"/></td>
          <td><xsl:value-of select="year"/></td>
          <td><xsl:value-of select="price"/></td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>
</xsl:stylesheet>

NameAuthorPublisherCountryYearPrice

否则,您可以很容易地修改此代码,也可以修改我。

您离得不远

一般来说,XSLT是一种编程语言,正确且一致地进行缩进

此外,元素名称如
也会对自己造成伤害。这些元素应该被称为
之类的东西。实际上,您应该首先在XML中更改它。永远不要使用变量元素名

以下是您的尝试,注:

<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  version="1.0"
>
  <!-- 
    Error: XHTML is XML, not HTML. Use method="xml", not "html".
  -->
  <xsl:output 
      method="html" 
      encoding="utf-8" 
      doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" 
      doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  />

  <!-- 
    Error: You can't use "batman.xsl" here.
      You *already are* inside the XML file. Use match expressions
      to refer to specific nodes that the template should be used for.
  -->
  <xsl:template match="batman.xsl">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      </head>
      <body>
        <table border="1">
          <tr bgcolor="#ebebeb">
            <th>name</th>
            <th>author</th>
            <th>publisher</th>
            <th>country</th>
            <th>year</th>
            <th>price</th>
          </tr>
          <!--
            Common mistake: avoid <xsl:for-each>
              XSLT is a template-matching language. You should 
              write templates.
          -->
          <xsl:for-each select="comics/batman/issue">
            <tr>
              <td><xsl:value-of select="name" /></td>
              <td><xsl:value-of select="author" /></td>
              <td><xsl:value-of select="publisher" /></td>
              <td><xsl:value-of select="country" /></td>
              <td><xsl:value-of select="year" /></td>
              <td><xsl:value-of select="price" /></td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

名称
作者
出版商
国家
年
价格
更好的方法(需要将所有
元素替换为
):


漫画列表
漫画
名称
作者
出版商
国家
年
价格

如果你花了10个小时在这上面,你肯定有一些XSLT代码来展示你所做的尝试?是的,我没有做任何尝试,因为我知道它们很可能是垃圾,是的。但是如果你已经挣扎了10个小时,那么这个概念本身就有问题。如果我们能看到你的代码,就更容易帮助你理清思路。好了,这是我最后一次尝试,但我仍然没有遵循。我先做了XML,然后做了DTD,我试着在另一个例子中使用DTD。那么,如果我的XML是正确的,那么集成的DTD应该是什么呢?这是否使您需要更改的XSL?所以我只需要对整个表使用通配符?我忘了提到它必须有元素,尽管一个作者必须有彩色背景,并且表需要边框。当我试图添加一个时,整张桌子都乱了?那么DTD还需要修改吗?出什么问题了?谢谢你的帮助@Tomalak,但对我来说还是一清二楚。我就是不能掌握这个过程。我想我会问我的导师。我原以为我的课程会先教我html,但他们希望我们不上一课就知道如何编写代码,而且很难理解这个理论。再次感谢。@jimbojones如果您刚刚开始学习HTML,那么XSLT已经超出了IMHO的范围。
<xsl:stylesheet 
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns="http://www.w3.org/1999/xhtml"
>
  <xsl:output 
      method="xml" 
      encoding="utf-8"
      doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" 
      doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  />

  <!-- create the base output document -->
  <xsl:template match="/comics">
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Comic List</title>
      </head>
      <body>
        <h1>Comics</h1>
        <xsl:apply-templates select="*" />
      </body>
    </html>
  </xsl:template>

  <!-- any children of the "comics" element get a table -->
  <xsl:template match="comics/*">
    <table border="1">
      <caption><xsl:value-of select="name()" /></caption>
      <tr>
        <td>Name</td>
        <td>Author</td>
        <td>Publisher</td>
        <td>Country</td>
        <td>Year</td>
        <td>Price</td>
      </tr>
      <xsl:apply-templates select="issue" />
    </table>
  </xsl:template>

  <!-- any issue becomes a table row -->
  <xsl:template match="issue">
    <tr>
      <td><xsl:apply-templates select="name" /></td>
      <td><xsl:apply-templates select="author" /></td>
      <td><xsl:apply-templates select="publisher" /></td>
      <td><xsl:apply-templates select="country" /></td>
      <td><xsl:apply-templates select="year" /></td>
      <td><xsl:apply-templates select="price" /></td>
    </tr>
  </xsl:template>

  <!-- 
    We don't need to specify a template for <name>, <author>, etc
    because XSLT defaults to "if there's no template defined, copy 
    the text to the output - which is what we want.
    But *if* you want to make the <name> bold, for example,
    all you need to do is create a <xsl:template match="name">.
  -->

</xsl:stylesheet>