【问题标题】:Add Google Font to <head> in XSLT在 XSLT 中将 Google 字体添加到 <head>
【发布时间】:2016-01-20 20:10:00
【问题描述】:

我正在使用 Google Search Appliance 过滤搜索结果,并且该页面是用 XSLT 编写的。我想链接到&lt;head&gt; 中的Google 字体但一直无法管理。

HTML:

<head>
    <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,600' rel='stylesheet' type='text/css'>
</head>

XSL 我试过了:

<xsl:template name="addOpenSansStart">
  <xsl:text disable-output-escaping="yes">&lt;head&gt;</xsl:text>
  <xsl:element name="link">
    <xsl:attribute name="href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,600'">href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,600'</xsl:attribute>
    <xsl:attribute name="rel='stylesheet'">rel='stylesheet'</xsl:attribute>
    <xsl:attribute name="type='text/css'">type='text/css'</xsl:attribute>
</xsl:element>
</xsl:template>
<xsl:template name="addOpenSansEnd">
  <xsl:text disable-output-escaping="yes">&lt;/head&gt;</xsl:text>
  <xsl:text>
  </xsl:text>
</xsl:template>

还有:

<xsl:template name="addOpenSansStart">
  <xsl:text disable-output-escaping="yes">&lt;head&gt;</xsl:text>
  <xsl:text disable-output-escaping="yes">&lt;link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,600' rel='stylesheet' type='text/css' /&gt;</xsl:text>
</xsl:template>
<xsl:template name="addOpenSansEnd">
  <xsl:text disable-output-escaping="yes">&lt;/head&gt;</xsl:text>
  <xsl:text>
  </xsl:text>
</xsl:template>

两者都给了我错误元素类型“链接”必须由匹配的结束标记“&lt;/link&gt;”终止。

【问题讨论】:

  • 您是否尝试生成 (即关闭链接标签)?应该工作
  • 第一个代码块的错误消息是:ERRORS 第 3738 行:属性名称 {href='fonts.googleapis...}不是有效的 QName 第 3739 行:属性名称 {rel='stylesheet'} 不是有效的 QName 第 3740 行:属性名称 {type='text/css'} 不是有效的 QName 警告 i> 第 6 行:使用 XSLT 2.0 处理器运行 XSLT 1.0 样式表 第 2261 行:没有后续同级指令的变量无效
  • 我意识到我在我的 XSL 代码下方发布了 HTML 以供参考,它正在生成第一个错误。
  • 我正在根据另一个 XSL 模板格式化我的代码,现在我看到它在文档的不同部分被调用。我不知道在哪里调用我的 addOpenSansStart 模板,所以我最终在&lt;meta name="robots" content="NOINDEX,NOFOLLOW"/&gt; 之后添加了我的 ,我知道它正在被拉入脑海并且它现在正在工作。感谢您的回复!

标签: xml xslt google-font-api google-search-appliance


【解决方案1】:

您会很高兴得知您可以简单地包含要生成的文字标记:

<xsl:template name="addOpenSansStart">
  <head>
    <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,600' 
          rel='stylesheet' type='text/css'/>
  </head>
  <!-- the rest of your template -->
</xsl:template>

如果您真的需要使用xsl:elementxsl:attribute,请意识到name 属性也不应该包含属性的值:

<xsl:template name="addOpenSansStart">
  <head>
    <xsl:element name="link">
      <xsl:attribute name="href">https://fonts.googleapis.com/css?family=Open+Sans:400,300,600</xsl:attribute>
      <xsl:attribute name="rel">stylesheet</xsl:attribute>
      <xsl:attribute name="type">text/css</xsl:attribute>
    </xsl:element>
  </head>
  <!-- the rest of your template -->
</xsl:template>

任何一种方式都适合你。

【讨论】:

  • 我不确定典型的 Google Search Appliance xslt 文档是什么样的,但我正在定制的文档无处不在。代码的格式与我习惯在 HTML 文档中看到的方式不同。但是,如果我曾经使用香草文档,我会记住这一点。谢谢!
猜你喜欢
  • 1970-01-01
  • 2017-07-11
  • 2020-12-01
  • 2011-08-16
  • 2015-07-07
  • 1970-01-01
  • 1970-01-01
  • 2020-02-18
  • 2023-04-10
相关资源
最近更新 更多