URL 中的井号标记和查询字符串交换
hashtag and query string interchange in the URL
我的URL既有hsahtag来打开隐藏和显示,又有跟踪URL(查询字符串),如下所示 http://pre-live/en-gb/information/help-and-contacts/contact-us#Group?clickpage=information-en--group-travel--index&kmtag=c&ban=||SH|2|CTA1|||||营地ID||||L2
|||||||问题是 #Group 由于它所包含的查询字符串而无法正常工作,当我们最终放置哈希标签时,它将起作用。
像这样 http://pre-live/en-gb/information/help-and-contacts/contact-us?clickpage=information-en--group-travel--index&kmtag=c&ban=||SH|2|CTA1|||||营地ID||||L2|||||||#Group
所以任何人都可以对此提出建议。
下面是我的 XSLT 部分
<div class="linkContainer">
<!-- Check if the URL we're tracking is internal (BA) or external, if it is internal it will return true-->
<xsl:variable name="trackingURL">
<xsl:apply-templates select="." mode="trackingURL">
<xsl:with-param name="trackURL" select="linkURL" />
</xsl:apply-templates>
</xsl:variable>
<xsl:variable name="link">
<xsl:value-of select="linkURL"/>
</xsl:variable>
<xsl:variable name="hash">
<xsl:value-of select="substring-after($link, '#')"/>
</xsl:variable>
<xsl:variable name="linkURL">
<xsl:if test="contains($link, '#')">
<xsl:value-of select="substring-before($link, '#')"/>
</xsl:if>
<xsl:if test="not(contains($link, '#'))">
<xsl:value-of select="$link"/>
</xsl:if>
</xsl:variable>
<a href="$PAGE_LINK[{$linkURL}]" title="{$titleTag}">
<xsl:if test="linkOpenStyle = 'newWindow'">
<xsl:attribute name="target">_blank</xsl:attribute>
</xsl:if>
<xsl:attribute name="class">
<xsl:if test="linkStyle='primary'">ui-priority-primary <xsl:value-of select="$linkStyleVal"/></xsl:if>
<xsl:if test="linkStyle='secondary'">ui-priority-secondary <xsl:value-of select="$linkStyleVal"/></xsl:if>
<xsl:if test="linkStyle='text'"><xsl:value-of select="$linkStyleVal"/></xsl:if>
</xsl:attribute>
<!-- Tracking params -->
<!-- It uses "self::node()" to apply the template to itself. We use that instead of "." because you can't use predicates on that. The params add the data attributes needed for tracking to work -->
<xsl:apply-templates select="self::node()[$tracking = 'yes' and $trackingURL = 'true']" mode="trackingParams">
<xsl:with-param name="click-position">
<xsl:apply-templates mode="buttonType" />
</xsl:with-param>
</xsl:apply-templates>
<!-- End Tracking -->
<xsl:value-of select="linkText" disable-output-escaping="yes" />
</a>
</div>
现在我已经从 url 中删除了主题标签,我不确定将其放置在 href 值中的位置和位置。
需要注意的一点是,您可以在现有代码中使用xsl:choose
,以避免重复contains
测试。
<xsl:variable name="linkURL">
<xsl:choose>
<xsl:when test="contains($link, '#')">
<xsl:value-of select="substring-before($link, '#')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$link"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
话虽如此,您可以在单个语句中实现您想要的。要获取hash
您需要获取"#"之后的文本,但您需要满足它是在查询字符串之前还是之后。诀窍是在表达式中使用的链接后附加一个虚拟的"?"。
<xsl:variable name="hash" select="substring-before(substring-after(concat($link, '?'), '#'), '?')" />
您可以使用类似的 to 技巧来获取没有哈希的 URL 的其余部分。这有两个部分,一部分用于获取哈希之前的部分,另一部分用于获取哈希之后的部分。
<xsl:variable name="linkURL">
<xsl:value-of select="substring-before(concat($link, '#'), '#')" />
<xsl:value-of select="substring-after($link, concat('#', $hash))" />
</xsl:variable>
如果不存在哈希,则第一条语句获取整个 URL,第二条语句不返回任何内容。
例如,给定此 XML
<links>
<link>
<linkURL>http://pre-live/en-gb/information/help-and-contacts/contact-us#Group?clickpage=information-en--group-travel--index&kmtag=c&ban=||SH|2|CTA1|||||CampID||||L2|||||||</linkURL>
</link>
<link>
<linkURL>http://pre-live/en-gb/information/help-and-contacts/contact-us?clickpage=information-en--group-travel--index&kmtag=c&ban=||SH|2|CTA1|||||CampID||||L2|||||||#Group</linkURL>
</link>
<link>
<linkURL>http://pre-live/en-gb/information/help-and-contacts/contact-us?clickpage=information-en--group-travel--index&kmtag=c&ban=||SH|2|CTA1|||||CampID||||L2|||||||</linkURL>
</link>
</links>
以下样式表
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="link">
<xsl:variable name="link" select="linkURL"/>
<xsl:variable name="hash" select="substring-before(substring-after(concat($link, '?'), '#'), '?')" />
<xsl:variable name="linkURL">
<xsl:value-of select="substring-before(concat($link, '#'), '#')" />
<xsl:value-of select="substring-after($link, concat('#', $hash))" />
</xsl:variable>
<xsl:copy>
<linkURL>
<xsl:value-of select="$linkURL" />
</linkURL>
<hash>
<xsl:value-of select="$hash" />
</hash>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
生成此输出
<link>
<linkURL>http://pre-live/en-gb/information/help-and-contacts/contact-us?clickpage=information-en--group-travel--index&kmtag=c&ban=||SH|2|CTA1|||||CampID||||L2|||||||</linkURL>
<hash>Group</hash>
</link>
<link>
<linkURL>http://pre-live/en-gb/information/help-and-contacts/contact-us?clickpage=information-en--group-travel--index&kmtag=c&ban=||SH|2|CTA1|||||CampID||||L2|||||||</linkURL>
<hash>Group</hash>
</link>
<link>
<linkURL>http://pre-live/en-gb/information/help-and-contacts/contact-us?clickpage=information-en--group-travel--index&kmtag=c&ban=||SH|2|CTA1|||||CampID||||L2|||||||</linkURL>
<hash/>
</link>
- 如何有效地将游戏数据存储在URL查询字符串中
- 从查询字符串参数推断出正确的数据类型
- AngularJS:获取链接中没有哈希的查询字符串值
- AngularJS,如何更改查询字符串
- 如何将activeadmin查询字符串传递给batch_action
- 如何从查询字符串中的输入字段发回文本
- Django在使用AJAX时没有接收到新的查询字符串
- 使用查询字符串方法清除浏览器缓存
- 用于刷新的Javascript更改查询字符串
- 我想将查询字符串变量添加到JSON名称/值对中
- 将查询字符串值从页面传递 asp.net 到 iframe
- jqueryajax生成查询字符串,如果未定义则不通过
- 如何将查询字符串传递到HTML框架
- AngularJs将查询字符串发布到c#控制器
- Jquery - 从查询字符串中获取“#”值之后,而不是在“?”符号值之后获取
- 在 JavaScript 中将查询字符串传递给 Web 服务 (ASP.NET)
- 在查询字符串中查找 where 子句
- POST encodeURI查询字符串内部服务器错误 500
- JavaScript:从具有使用 # 符号的路由的 URL 获取查询字符串
- 更改链接 URL 中的路径名,同时保持查询字符串不变