问题描述
我正在探索用于生成Web内容的XML - > XSLT - > HTML meme。我很少有XSLT经验。
我很好奇XSLT有哪些机制可用于处理抽象或重构。
例如,在通用HTML和服务端包含的情况下,可以将许多页面模板化并分解到常见的页眉,导航和页脚段,并且页面本身基本上是本体。 / p>
常用标记语言JSP,PHP,ASP尽可能让所有这些段都具有动态内容(例如将用户名添加到每个标题块)。
通过允许您创建标记文件,JSP可以更进一步,标记文件可以接受在生成内容时使用的参数,甚至可以围绕并处理标签本身。
我很好奇类似的功能是在XSLT中完成的。有什么设施可以创建可重用的XSLT块用于创建HTML页面?
我分开了我的网页。有一个template.xsl文件被我的每个XSL导入。大多数页面只有template.xsl,但是一些页面(如购物车等)需要自己的
,因为它们解析的数据类型不同。
< page title =Home>
<导览>
< / navigation>
< main>
< / main>
< / page>
这是我的template.xsl中的一个片段。我在这里扔了所有常见的东西,然后给我的页面
给我的页面添加他们自己的信息,通过 call-template 。
< xsl:template match =/ pagename =page>
< html>
< head>
< title>< xsl:value-of select =(@ title)/>< / title>
< xsl:call-template name =css/>
< xsl:call-template name =script/>
< / head>
< body>
< xsl:call-template name =container/>
< / body>
< / html>
< / xsl:template>
一个关于我的css标签如何响应的例子。请注意,它调用 css-extended。 css只有
具有适用于所有页面的通用CSS。一些页面需要更多。那些
可以覆盖css-extended。请注意,如果
页面调用一个模板但没有在任何地方定义它, call-template 将会失败。
< xsl:template name =css>
< link rel =stylesheettype =text / csshref ={$ cssPath} reset.css/>
< link rel =stylesheettype =text / csshref ={$ cssPath} style.css/>
< link rel =stylesheettype =text / csshref ={$ cssPath} layout.css/>
< xsl:call-template name =css-extended/>
< / xsl:template>
< xsl:template name =css-extended/>
我的容器可以以类似的方式工作 - 定义了常见的东西,然后每个页面
可以提供一个实现。默认实现在XSL中。 ( content )
< xsl:template name =container >
< div id =container>
< xsl:call-template name =header/>
< xsl:call-template name =content/>
< xsl:call-template name =footer/>
< / div>
< / xsl:template>
< xsl:template name =content>
< div id =content>
< div id =content-inner>
< xsl:call-template name =sideBar/>
< xsl:call-template name =main/>
< / div>
< / div>
< / xsl:template>
< xsl:template name =main>
< div id =main>
< xsl:apply-templates select =main/>
< xsl:call-template name =main-extended/>
< / div>
< / xsl:template>
< xsl:template name =main-extended/>
< xsl:template name =footer>
< div id =footer>
< div id =footer-inner>
< / div>
< / div>
< / xsl:template>
它对我来说非常合适。如果有任何问题我可以为您解答,请告诉我。
I'm exploring the XML -> XSLT -> HTML meme for producing web content. I have very little XSLT experience.
I'm curious what mechanisms are available in XSLT to handle abstractions or "refactoring".
For example, with generic HTML and a service side include, many pages can be templated and decomposed to where there are, say, common header, nav, and footer segments, and the page itself is basically the body.
The common markup languages, JSP, PHP, ASP, go as far as to allow all of those segments to have dynamic content (such as adding the user name to every header block).
JSP goes even farther by allowing you to create Tag files, which can accept arguments to be used when generating the content, and even surround and work on content within the tags themselves.
I'm curious similar functionality is done within XSLT. What facilities are there to make reusable block of XSLT for things like creating HTML pages?
For my own project, this is how I divided up my pages. There was a template.xsl file which was importedby each of my XSLs. Most pages just had template.xsl, but some pages such as cart, etc. needed their ownbecause of the different kind of data they were parsing.
<page title="Home"> <navigation> <!-- something here --> </navigation> <main> <!-- something here --> </main> </page>
This is a snippet from my template.xsl. I threw in all the common stuff in here, and then gave the opportunityfor my pages to add their own information through call-template.
<xsl:template match="/page" name="page"> <html> <head> <title><xsl:value-of select="(@title)" /></title> <xsl:call-template name="css" /> <xsl:call-template name="script" /> </head> <body> <xsl:call-template name="container" /> </body> </html> </xsl:template>
An example of how my css tag would respond. Note that it calls css-extended. css onlyhad the the common css' that would apply across all pages. Some pages needed more. Thosecould override css-extended. Note that is needed because call-template will fail if a page calls a template but doesn't define it anywhere.
<xsl:template name="css"> <link rel="stylesheet" type="text/css" href="{$cssPath}reset.css" /> <link rel="stylesheet" type="text/css" href="{$cssPath}style.css" /> <link rel="stylesheet" type="text/css" href="{$cssPath}layout.css" /> <xsl:call-template name="css-extended" /> </xsl:template> <!-- This is meant to be blank. It gets overriden by implementing stylesheets --> <xsl:template name="css-extended" />
My container would work in a similar manner-- common stuff was defined and then each pagecould just provide an implementation. A default implementation was in the XSL. (in content)
<xsl:template name="container"> <div id="container"> <xsl:call-template name="header" /> <xsl:call-template name="content" /> <xsl:call-template name="footer" /> </div> </xsl:template> <xsl:template name="content"> <div id="content"> <div id="content-inner"> <xsl:call-template name="sideBar" /> <xsl:call-template name="main" /> </div> </div> </xsl:template> <xsl:template name="main"> <div id="main"> <xsl:apply-templates select="main" /> <xsl:call-template name="main-extended" /> </div> </xsl:template> <!-- This is meant to be blank. It gets overriden by implementing stylesheets --> <xsl:template name="main-extended" /> <xsl:template name="footer"> <div id="footer"> <div id="footer-inner"> <!-- Footer content here --> </div> </div> </xsl:template>
It worked quite beautifully for me. If there are any questions I can answer for you, let me know.
这篇关于XSLT抽象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!