【发布时间】:2013-12-09 03:54:33
【问题描述】:
如果我的问题太简单,不属于这里,我很抱歉。我正在处理一项任务,我有一个 XML 源文档,其中包含一个样式表声明,该声明链接到一个输出为 HTML 的 XSL。现在一切正常,但是这个页面需要是不同页面上的链接。这个其他页面(我假设我会将其编码为一个简单的 HTML 文件)还需要第二个链接,该链接将使用来自我已经在使用的同一 XML 源文档的信息执行一些计算。我想我遗漏了一些简单的东西,但是如何使用相同的 XML 创建另一个页面?
XML:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="cookies.xsl" ?>
<cookies>
<cookie>
<brand>Oreo</brand>
<name>Double Stuff</name>
<nutritional_info>
<calories>150</calories>
<fat>20</fat>
<sugar>5</sugar>
<protein>1</protein>
</nutritional_info>
</cookie>
<cookie>
<brand>Oreo</brand>
<name>Golden Oreo</name>
<nutritional_info>
<calories>190</calories>
<fat>7.6</fat>
<sugar>13.7</sugar>
<protein>1.5</protein>
</nutritional_info>
</cookie>
<cookie>
<brand>Oreo</brand>
<name>Sandwich Cookie</name>
<nutritional_info>
<calories>140</calories>
<fat>7.0</fat>
<sugar>13.0</sugar>
<protein>1.0</protein>
</nutritional_info>
</cookie>
<cookie>
<brand>Archway</brand>
<name>Dutch Cocoa</name>
<nutritional_info>
<calories>110</calories>
<fat>3.6</fat>
<sugar>16.0</sugar>
<protein>1.1</protein>
</nutritional_info>
</cookie>
XSL:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method ="html" version="4.0" />
<xsl:template match="/">
<html>
<head>
<title>Cookies</title>
<link href="cookies.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1 id="header"><img src="header.jpg" alt="cookies" height="150" width="100%"/></h1>
<h1>Nutritional Info</h1>
<h2 id="n_info">
<xsl:apply-templates select="cookies/cookie"/>
</h2>
</body>
</html>
</xsl:template>
<xsl:template match="cookie">
<h2><xsl:value-of select="@brand"/> <xsl:value-of select="name"/></h2>
<xsl:for-each select=".">
<p><img src="{name}.jpg" id="picture"/>
</p>
<xsl:apply-templates select="nutritional_info"/>
</xsl:for-each>
</xsl:template>
<xsl:template match="nutritional_info">
<table border="1" id="info">
<tr>
<th>Calories</th>
<th>Protein</th>
<th>Total Fat</th>
<th>Sugar</th>
</tr>
<tr>
<xsl:apply-templates select="calories"/>
<xsl:apply-templates select="protein"/>
<xsl:apply-templates select="fat"/>
<xsl:apply-templates select="sugar"/>
</tr>
</table>
</xsl:template>
<xsl:template match="calories|protein|fat|sugar">
<td><xsl:value-of select="."/></td>
</xsl:template>
我想创建一个链接,该链接将使用此样式表打开 xml,然后创建另一个链接,该链接将应用不同的样式和模板,但使用来自同一 XML 文件的相同信息。
【问题讨论】:
-
"如何使用相同的 XML 创建另一个页面" - 我建议编写一些 XSLT 来将一些 输入 XML 转换为 输出 HTML。如果您已经拥有这 3 样东西,也许您可以向我们展示,我们可以提供更多帮助。
-
我认为这无关紧要,但用该信息更新了 OP。
-
当然很重要。我们需要查看您的代码来诊断问题。现在,这段代码有什么不应该做的?
-
@LegoStormtroopr 请阅读这个问题,很清楚(从一开始就很清楚)。
-
看看这是否有帮助:w3schools.com/xsl/xsl_client.asp