【问题标题】:How to use same XML source document for multiple webpages using XSL如何使用 XSL 为多个网页使用相同的 XML 源文档
【发布时间】: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

标签: html xml xslt xslt-1.0


【解决方案1】:

有几种方法可以实现这一点:动态输入文件、使用 XSLT 2.0 功能或仅拥有两个 XML 文件。

动态输入

如果您正在动态创建输入 XML 文件(即在事件发生时刷新它),请确保以下行:

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

引用正确的 XSLT 样式表,根据单击的链接。

XSLT 功能

否则,可以使用result-document() 函数从同一个样式表中输出多个 HTML 文件。但是,只有当您可以使用 XSLT 2.0 时,才可以选择此选项(请参阅:XSLT: :result-document)。从您的样式表来看,我假设您不使用 XSLT 2.0 处理器。

即使 XSLT 1.0 不包括输出多个文件,EXSLT 也可以(参见例如:Splitting XML into multiple files with XSLT)。

复制并重命名您的 XML

作为最后的手段,手动操作您的输入 XML。复制 XML 数据并更改上面引用的引用行。确保cookies1.xml 包含

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

并且cookies2.xml 包含

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

请注意,这个问题的出现只是因为您在 XML 文件中对样式表的引用进行了硬编码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    相关资源
    最近更新 更多