XSLT获取字符串或网址的一部分

时间:2011-10-17 12:22:04

标签: xml xslt xslt-1.0

我在XML文件中得到以下XML元素。从这个元素我需要检索URL信息。

<Page SmallImage="" LargeImage="" Icon="" MenuText="Text" MouseOver="" Image="" 
ImageActive="" ImageMouseOver="" Allowclick="True" ShowInSitemap="True" 
Href="Default.aspx?ID=27&GroupID=GROUP11" 
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"/>

如上例所示,属性'Href'包含GroupID。

Href="Default.aspx?ID=27&GroupID=GROUP11"

现在我想将URL的GROUPID与FriendlyHref属性中的值连接起来。是否可以使用XSLT1.0获取此值?

我想得到的结果是;

/nl-nl/assortiment/group/category/text.aspx?Group=GROUP11

我已经在stackoverflow上找到了thisthis主题,但这些示例并没有给我任何结果。

2 个答案:

答案 0 :(得分:2)

如果GroupID始终是查询字符串的最后一个参数,则可以使用substring-after()来提取值。例如:

<xsl:template match="Page">
    <a>
        <xsl:attribute name="href">
            <xsl:value-of select="concat(@FriendlyHref, '?Group=', substring-after(@Href, 'GroupID='))" />
        </xsl:attribute>
    </a>
</xsl:template>

答案 1 :(得分:2)

此转换正确获取查询字符串值,无论其位置如何(在开头,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('&amp;',substring-after(@Href, '?'), '&amp;')"/>

    <xsl:variable name="vWantedValue"   select=
     "substring-before
         (substring-after($vQueryStrings, '&amp;GroupID='),
         '&amp;')"/>
    <xsl:value-of select="concat(@FriendlyHref, '?',$vWantedValue)"/>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档(略微更正,以使其更具挑战性,同时也做得更好):

<Page SmallImage="" LargeImage="" Icon=""
     MenuText="Text" MouseOver="" Image=""
     ImageActive="" ImageMouseOver="" Allowclick="True"
     ShowInSitemap="True"
     Href="Default.aspx?ID=27&amp;GroupID=GROUP11&amp;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('&amp;',
                        substring-after(@*[name()=$pHrefName], '?'),
                        '&amp;')"/>

    <xsl:variable name="vWantedValue"   select=
     "substring-before
         (substring-after($vQueryStrings, concat('&amp;', $pQSName, '=')),
         '&amp;')"/>
    <xsl:value-of select="$vWantedValue"/>
 </xsl:template>
</xsl:stylesheet>

在上面的XML文档中应用此转换时,会产生所需的结果

GROUP11