gpt4 book ai didi

.net - 具有动态内容区域的 XSLT 布局

转载 作者:行者123 更新时间:2023-12-04 06:48:21 28 4
gpt4 key购买 nike

我正在重建我们网站的 UI 部分,该部分完全基于 javascript/ajax(没有充分的理由并且以相当低效的方式),这样后端现在将完成大部分内容生成。它是一个 C# .net 应用程序。

我们几乎所有的页面(大概有 40-50 页)都有相同的基本布局。我是 XSLT 的新手,但我已经使用 MVC 框架完成了很多工作,例如 Spring(java,使用 Sitemesh 进行布局)、Symfony (PHP)、一些 Rails 以及其他一些框架。我喜欢拥有一个或多个通用模板的能力,然后有一个特定的“内容”部分,页面特定的东西放在那里。我无法弄清楚这是如何使用 XSLT 完成的。在这个应用程序的情况下,我在支持 xslt 页面的 xml 中有一个可用的值,我们称之为 ContentXSL,其值是 xsl 文件的名称
我想用于页面的内容部分。我知道这是不可能的,但使用它会很好:

 <xsl:call-template name="{$ContentXSL}" />

然后我可以简单地把它放在内容部分..但是这是不可能的,所以我需要一个大量的选择语句,根据 ContentPage 变量调用正确的模板..这也意味着在我的 Layout.xsl 文件中我必须包含所有 40-50 个 xsl 文档。我认为开销会很大,但我不确定。如果网站获得大量流量,这样做是否合理?

这样做的其他常见方法是什么?似乎大多数现代框架都允许您使用这种模式来装饰内容。在 Symfony 的情况下,它运行得非常好并且非常灵活(带有插槽和所有这些)。

我知道另一个可能的解决方案是拥有 40 个独立文件,它们都具有类似的标记并包含特殊部分,如页眉和页脚。这意味着如果我想更改网站布局的整体结构,我必须编辑所有 40-50 个页面(非常烦人)。

更新——更多解释

我想进一步解释这一点,因为我有某些需要需要大量工程才能改变的要求。
首先,后端将传递给我一些 xml,它会让我知道查询参数位于网站的 URL 中。此外,它还会传递给我构建页面所需的数据(数据在商业数据的形式,没有 html 或类似的东西)。
数据看起来类似于:
<xml>
<section>Blogs</section>
<page>showAll</section>
<data>
<blogs>
<blog>
<author>somebody</author>
<title></title>
<content>..</content>
</blog>
</blog>..</blog>
</blogs>
</data>
</xml>

现在想要的是有一个这样的页面模板:
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:msxsl='urn:schemas-microsoft-com:xslt'>  
<xsl:output omit-xml-declaration='yes' method='html' media-type='text/html' indent='yes' />
<xsl:include href="Header.xsl"/>
<xsl:include href="Nav.xsl"/>
<xsl:template name='MainLayout' match='*'>
<html>
<head>
<title></title>
</head>
<body>
<div id="header"><xsl:call-template name="Header" /></div>
<div id="nav"><xsl:call-template name="Nav" /></div>
<div id="content">
[here is where i want to use the xsl from {/xml/section}/{/xml/page}.xsl]
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

现在,对于此页面的内容,我将拥有以下文件:
博客/showAll.xsl
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:msxsl='urn:schemas-microsoft-com:xslt'>  
<xsl:output omit-xml-declaration='yes' method='html' media-type='text/html' indent='yes' />
<xsl:template name='Blogs_ShowAll'>
<div id="blogs-showAll">
..iterate over /xml/data/blogs converting to html
</div>
</xsl:template>
</xsl:stylesheet>

迄今为止的解决方案都很好,但只有其中一个我能够完全消化(提到包括所有 xsl 文件并使用 xsl:choose 选择正确的一个)。
我不确定如何将 FXSL 方法应用于手头的问题。
请注意,我不反对使用 sitemesh 类型的方法,我指定 html/body 标签和所有在 child 中的内容,并将我在 child 的 body 部分中的内容替换为布局的内容 div(此外,如果 child 中有一个标题标签替换布局中的标题标签 - 类似的东西)。

最佳答案

OP 提供了他的问题的其他详细信息,此答案提供了现在要求的其他解决方案。

一、思路:

本次改造 :

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
<html>
<xsl:apply-templates select="*/page"/>
</html>
</xsl:template>

<xsl:template match="page[. = 'showAll']">
<!-- Transform all data to html -->
<xsl:apply-templates select="../*/blogs" mode="showAll"/>
</xsl:template>

<xsl:template match="page[. = 'showBrief']">
<!-- Transform the data to Summary html -->
<xsl:apply-templates select="../*/blogs" mode="showBrief"/>
</xsl:template>

<xsl:template match="blogs" mode="showAll">
<h1>All Blogs: </h1>
<table border="1">
<xsl:apply-templates mode="showAll"/>
</table>
</xsl:template>

<xsl:template match="blog" mode="showAll">
<tr>
<td>Blog of <xsl:value-of select="author"/></td>
<td><xsl:value-of select="title"/></td>
</tr>
<tr>
<td colspan="2"><xsl:apply-templates select="content/node()" mode="showAll"/></td>
</tr>
<xsl:if test="not(position()=last())">
<tr><td colspan="2">&#xA0;</td></tr>
</xsl:if>
</xsl:template>

<xsl:template match="blogs" mode="showBrief">
<h1>Blogs Summary: </h1>
<table border="1">
<xsl:apply-templates mode="showBrief"/>
</table>
</xsl:template>

<xsl:template match="blog" mode="showBrief">
<tr>
<td>
<xsl:value-of select="concat(author, ': ', title)"/>
</td>
</tr>
</xsl:template>

</xsl:stylesheet>

应用于此 XML 文档时 (基于提供的 XML 文本,但使其格式良好且更加充实):
<xml>
<section>Blogs</section>
<page>showAll</page>
<data>
<blogs>
<blog>
<author>John Smith</author>
<title>All about golden fish</title>
<content>
Here I publish my latest achievements
in raising golden fish.
</content>
</blog>
<blog>
<author>Mary Jones</author>
<title>Knitting, Knitting, Knitting</title>
<content>
How to knit a sharf.
</content>
</blog>
</blogs>
</data>
</xml>

产生所需的“全部显示”类型的输出 :
<html>
<h1>All Blogs: </h1>
<table border="1">
<tr>
<td>Blog of John Smith</td>
<td>All about golden fish</td>
</tr>
<tr>
<td colspan="2">
Here I publish my latest achievements
in raising golden fish.

</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td>Blog of Mary Jones</td>
<td>Knitting, Knitting, Knitting</td>
</tr>
<tr>
<td colspan="2">
How to knit a sharf.

</td>
</tr>
</table>
</html>

现在我们更改 XML 文档并替换 page元素与此 :
 <page>showBrief</page>

当对更新的 XML 文档应用相同的转换时,它现在会生成所需的摘要输出 :
<html>
<h1>Blogs Summary: </h1>
<table border="1">
<tr>
<td>John Smith: All about golden fish</td>
</tr>
<tr>
<td>Mary Jones: Knitting, Knitting, Knitting</td>
</tr>
</table>
</html>

二、下一步

在实践中,给定模式下的所有模板都将位于其单独的 xsl 文件中,并将由主样式表导入:

转换(主要样式表)因此变成 :
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="showAll.xsl"/>
<xsl:import href="showBrief.xsl"/>

<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
<html>
<xsl:apply-templates select="*/page"/>
</html>
</xsl:template>
</xsl:stylesheet>

请注意 :
  • 转换事先不知道将应用哪些模板 ——转型完全是数据驱动的。
  • 现在不存在的模板,以后可以写,会应用无需更改主要样式表。
  • 没有条件逻辑 , <xsl:choose>说明等这就是 xsl 模板的真正力量 .
  • 此转换基于与 FXSL 基于 相同的想法。 .
  • 关于.net - 具有动态内容区域的 XSLT 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3473108/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com