为不同的输出重复使用一组 XSLT 条件(在本例中为 amp-analytics 配置)
Reuse a set of XSLT conditions for different output (in this case amp-analytics configuration)
这是一个地狱般的技术组合,但我别无选择。我需要在使用 XSLT 呈现的 AMP HTML 页面上输出一个 amp-analytics
配置对象。配置文档 here 以防万一。
看这部分配置。您需要为每个需要发送到分析的变量在 "vars" 属性中输出一个键。但是您还需要在请求查询字符串中包含他们的名字。可以有多个请求。
{
"requests": {
"pageview": "https://example.com/analytics?url=${canonicalUrl}&title=${title}&acct=${account}&clientId=${clientId(cid-scope)}",
},
"vars": {
"account": "ABC123",
"title": "Homepage"
}
}
我有一个 XML 变量列表:
<vars>
<account>111</account>
<title>222</title>
<foo>333</foo>
</vars>
在我的 XSLT 中,我是这样的:
<xsl:if test="/vars/account">
"account": "<xsl:value-of select="/vars/account" />",
</xsl:if>
<xsl:if test="/vars/title">
"title": "<xsl:value-of select="/vars/title" />",
</xsl:if>
<xsl:if test="/vars/foo">
"foo": "<xsl:value-of select="/vars/foo" />",
</xsl:if>
好吧,这很丑但很好。但是现在我必须在 requests
属性(如 &title=${title}&acct=${account}&cd1=${foo}
)中的每个请求中明确包含所有这些变量。我在这里迷路了。
我不想复制所有用于输出查询字符串的逻辑。我想到的唯一一件事就是制作一个带有参数的命名模板并为每个请求调用它:
<xsl:template name="vars">
<xsl:param name="for_query" select="1"/>
<xsl:if test="/vars/account">
<xsl:choose>
<xsl:when test="$for_query = 1">&account=${account}</xsl:when>
<xsl:otherwise>"account": "<xsl:value-of select="/vars/account" />",</xsl:otherwise>
</xsl:choose>
</xsl:if>
<xsl:if test="/vars/title">
<xsl:choose>
<xsl:when test="$for_query = 1">&title=${title}</xsl:when>
<xsl:otherwise>"title": "<xsl:value-of select="/vars/title" />",</xsl:otherwise>
</xsl:choose>
</xsl:if>
<xsl:if test="/vars/foo">
<xsl:choose>
<xsl:when test="$for_query = 1">&foo=${foo}</xsl:when>
<xsl:otherwise>"foo": "<xsl:value-of select="/vars/foo" />",</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
想象一下,把它放长几倍,稍微复杂一点,你就不会奇怪为什么它让我的眼睛流血了。然后我在适当的地方称呼它:
{
"requests": {
"pageview": "https://example.com/analytics?url=${canonicalUrl}<xsl:call-template name="vars" />",
"event": "https://example.com/analytics?url=${canonicalUrl}<xsl:call-template name="vars" />"
},
"vars": {
<xsl:call-template name="vars">
<xsl:with-param name="for_query" select="0"/>
</xsl:call-template>
"look_ma": "no_comma"
}
}
您推荐任何不那么野蛮的解决方案吗?
您尝试过使用 extraUrlParams
吗?他们应该让你在不触及 request
和 vars
块的情况下指定参数。
这是一个地狱般的技术组合,但我别无选择。我需要在使用 XSLT 呈现的 AMP HTML 页面上输出一个 amp-analytics
配置对象。配置文档 here 以防万一。
看这部分配置。您需要为每个需要发送到分析的变量在 "vars" 属性中输出一个键。但是您还需要在请求查询字符串中包含他们的名字。可以有多个请求。
{
"requests": {
"pageview": "https://example.com/analytics?url=${canonicalUrl}&title=${title}&acct=${account}&clientId=${clientId(cid-scope)}",
},
"vars": {
"account": "ABC123",
"title": "Homepage"
}
}
我有一个 XML 变量列表:
<vars>
<account>111</account>
<title>222</title>
<foo>333</foo>
</vars>
在我的 XSLT 中,我是这样的:
<xsl:if test="/vars/account">
"account": "<xsl:value-of select="/vars/account" />",
</xsl:if>
<xsl:if test="/vars/title">
"title": "<xsl:value-of select="/vars/title" />",
</xsl:if>
<xsl:if test="/vars/foo">
"foo": "<xsl:value-of select="/vars/foo" />",
</xsl:if>
好吧,这很丑但很好。但是现在我必须在 requests
属性(如 &title=${title}&acct=${account}&cd1=${foo}
)中的每个请求中明确包含所有这些变量。我在这里迷路了。
我不想复制所有用于输出查询字符串的逻辑。我想到的唯一一件事就是制作一个带有参数的命名模板并为每个请求调用它:
<xsl:template name="vars">
<xsl:param name="for_query" select="1"/>
<xsl:if test="/vars/account">
<xsl:choose>
<xsl:when test="$for_query = 1">&account=${account}</xsl:when>
<xsl:otherwise>"account": "<xsl:value-of select="/vars/account" />",</xsl:otherwise>
</xsl:choose>
</xsl:if>
<xsl:if test="/vars/title">
<xsl:choose>
<xsl:when test="$for_query = 1">&title=${title}</xsl:when>
<xsl:otherwise>"title": "<xsl:value-of select="/vars/title" />",</xsl:otherwise>
</xsl:choose>
</xsl:if>
<xsl:if test="/vars/foo">
<xsl:choose>
<xsl:when test="$for_query = 1">&foo=${foo}</xsl:when>
<xsl:otherwise>"foo": "<xsl:value-of select="/vars/foo" />",</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
想象一下,把它放长几倍,稍微复杂一点,你就不会奇怪为什么它让我的眼睛流血了。然后我在适当的地方称呼它:
{
"requests": {
"pageview": "https://example.com/analytics?url=${canonicalUrl}<xsl:call-template name="vars" />",
"event": "https://example.com/analytics?url=${canonicalUrl}<xsl:call-template name="vars" />"
},
"vars": {
<xsl:call-template name="vars">
<xsl:with-param name="for_query" select="0"/>
</xsl:call-template>
"look_ma": "no_comma"
}
}
您推荐任何不那么野蛮的解决方案吗?
您尝试过使用 extraUrlParams
吗?他们应该让你在不触及 request
和 vars
块的情况下指定参数。