将 Head 部分存储在 Coldfusion 中,无需在 XSL 中进行硬编码



我正在尝试将 XSLT 1.0 的这一部分从使用元标记硬编码转换,以防将来我想更改它们。有没有办法在 ColdFusion 中显示包含样式表、关键字和描述的元标记?

我已经尝试过让它几乎可以与样式表一起使用,但它只在 <html> 上方或下方显示它</html>而不是在 <head> 内部,这是我需要它用于所有这些的地方。

关于我应该如何以这种方式显示它的任何建议?

.CFM

**

<cfset MyXmlFile = Expandpath("events.xml")>
<cffile action="READ" variable="xmlInput"  file="#MyXmlFile#">
<cfset MyXmlFile = Expandpath("events.xsl")>
<cffile action="READ" variable="xslInput" file="#MyXmlFile#">
<cfset xslParam = StructNew() >
<cfset xslParam["pram"] = "#url.pram#" >
<cfset xmlOutput = XMLTransform(xmlInput, xslInput, xslParam)>
<!--- data is output --->
<cfcontent type="text/html" reset="true" /><!DOCTYPE html>
<cfoutput>
<cfset style='<link rel="stylesheet" type="text/css" href="stylesheet.css">' />
#style#
#xmloutput#
</cfoutput>

**

XSLT

 <xsl:element name="meta"><xsl:attribute name="name">description</xsl:attribute><xsl:attribute name="content">Listings of all events</xsl:attribute></xsl:element>
      <xsl:element name="meta"><xsl:attribute name="name">keywords</xsl:attribute><xsl:attribute name="content">events, event, music, help, information</xsl:attribute></xsl:element>
      <xsl:element name="link"><xsl:attribute name="rel">icon</xsl:attribute><xsl:attribute name="href">images/favicon.ico</xsl:attribute><xsl:attribute name="type">image/x-icon</xsl:attribute></xsl:element>
      <xsl:element name="link"><xsl:attribute name="rel">shortcut icon</xsl:attribute><xsl:attribute name="href">images/favicon.ico</xsl:attribute><xsl:attribute name="type">image/x-icon</xsl:attribute></xsl:element>
      <xsl:element name="link"><xsl:attribute name="rel">stylesheet</xsl:attribute><xsl:attribute name="type">text/css</xsl:attribute><xsl:attribute name="href">stylesheet.css</xsl:attribute></xsl:element>

网页顶部

<!DOCTYPE html>
<link rel="stylesheet" type="text/css" href="stylesheet.css"> <html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <meta name="description" content="Listings of all events">
      <meta name="keywords" content="events, event, music, help, information">
      <link rel="icon" href="images/favicon.ico" type="image/x-icon">
      <link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon">
      <link rel="stylesheet" type="text/css" href="stylesheet.css">
      <title>London Comic Con</title>
   </head>
   <body>

XML 示例

<?xml version="1.0" encoding="ISO-8859-1"?>
<events 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="events.xsd">
    <venue id="01" vtitle="ExCeL Exhibition Centre" location="London" telephone="0844 448 7600">
    <about>The ExCel Exhibition Centre was opened in November 2000 and was built by Sir Robert MacAlpine. The venue was most recently bought over acquired by the Abu Dhabi National Exhibitions Company in 2008. Phase II was completed on 1 May 2010. This expansion created The International Convention Centre London (ICC London) adding to ExCeL's event space, as well as further meeting space and banqueting facilities.</about>
    <event name="London Comic Con" date="2013-10-12">
        <image>images/MCM1.jpg</image><attribute>London Anime Event</attribute>
        <description>A convention for all things Anime, video games and Japanese culture.</description>
        <keywords>events, event, music, help, information</keywords>
        <ticket_price type="adult" status="none">&#163;18.00</ticket_price>
        <ticket_price type="child" status="available">&#163;8.00</ticket_price>
        <ticket_price type="junior" status="available">&#163;0.00</ticket_price>
        <email>london@mcmexpo.net</email>
    </event>

您可以定义一个模板来匹配要为其生成的元素<meta>元素,并构造相应的<meta>元素及其属性。

此示例使用带有属性值模板的元素文本:

<xsl:template match="description | keywords" mode="meta">
  <meta name="{local-name()}" content="{.}"/>
</xsl:template>

在样式表中应用:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="/">
  <html>
    <xsl:call-template name="head"/>
    <!--body stuff goes here-->
  </html> 
</xsl:template>
<xsl:template name="head">
  <head>
    <xsl:apply-templates select="/events/venue/event/*" mode="meta"/>
    <link rel="icon" href="images/favicon.ico" type="image/x-icon"/>
    <link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" type="text/css" href="stylesheet.css"/>
  </head>
</xsl:template>
<!--template to match the elements that you want to produce meta elements for-->
<xsl:template match="description | keywords" mode="meta">
  <meta name="{local-name()}" content="{.}"/>
</xsl:template>
<!--for all other elements in this mode, do nothing -->
<xsl:template match="*" mode="meta"/>
</xsl:stylesheet>  

我通过在 ColdFusion 文件中添加创建cfhtmlhead部分来完成这项工作。根据文档:

使用此标记嵌入 JavaScript 代码,或放置其他 HTML 标记,例如 HTML 页眉中的元、链接、标题或基。

CFM - 示例

<cfhtmlhead text='<link rel="stylesheet" 
     type="text/css" 
     href="stylesheet.css">#chr(13)##chr(10)#'>

相关内容

  • 没有找到相关文章

最新更新