这种变换正确求出查询字符串值,而不管其位置的(在开始时,在midle或在包含字符串的末尾所有查询字符串):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<xsl:variable name="vQueryStrings"
select="concat('&',substring-after(@Href, '?'), '&')"/>
<xsl:variable name="vWantedValue" select=
"substring-before
(substring-after($vQueryStrings, '&GroupID='),
'&')"/>
<xsl:value-of select="concat(@FriendlyHref, '?',$vWantedValue)"/>
</xsl:template>
</xsl:stylesheet>
当施加到所提供的XML文档(略微校正,以使具有挑战性,并且它简洁(wellformed)):
<Page SmallImage="" LargeImage="" Icon=""
MenuText="Text" MouseOver="" Image=""
ImageActive="" ImageMouseOver="" Allowclick="True"
ShowInSitemap="True"
Href="Default.aspx?ID=27&GroupID=GROUP11&foo=bar"
FriendlyHref="/nl-nl/assortiment/group/category/text.aspx"
Title="" NavigationTag="" RelativeLevel="4" Sort="1"
LastInLevel="True" ChildCount="0" class="L4" ID="18"
AreaID="1" InPath="False" Active="False" AbsoluteLevel="4"/>
的想要的,正确的结果产生:
/nl-nl/assortiment/group/category/text.aspx?GROUP11
最后:一个完全通用/参数化的解决方案,它接受任何查询字符串名称作为参数,并产生其值:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<xsl:call-template name="getQueryString"/>
</xsl:template>
<xsl:template name="getQueryString">
<xsl:param name="pHrefName" select="'Href'"/>
<xsl:param name="pQSName" select="'GroupID'"/>
<xsl:variable name="vQueryStrings"
select="concat('&',
substring-after(@*[name()=$pHrefName], '?'),
'&')"/>
<xsl:variable name="vWantedValue" select=
"substring-before
(substring-after($vQueryStrings, concat('&', $pQSName, '=')),
'&')"/>
<xsl:value-of select="$vWantedValue"/>
</xsl:template>
</xsl:stylesheet>
将此转换应用于上述XML文档时,会生成想要的结果:
GROUP11
棒极了!正是我需要的。在这种情况下很好的解决方案,因为GroupID确实总是最后一个参数。 – Rob 2011-10-17 12:52:14
如果XML是这样:'And, Jo `。 http://stackoverflow.com/questions/29307212/how-to-get-certain-part-of-a-link-in-xslt –
SearchForKnowledge
2015-03-27 18:09:24