2011-10-17 56 views
3

我在XML文件中返回以下XML元素。从这个元素我需要检索URL信息。XSLT获取字符串或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 

我已经找到thisthis话题在这里计算器,但这些例子并没有给我任何结果的。

回答

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> 
+0

棒极了!正是我需要的。在这种情况下很好的解决方案,因为GroupID确实总是最后一个参数。 – Rob 2011-10-17 12:52:14

+0

如果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

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文档(略微校正,以使具有挑战性,并且它简洁(wellformed)):

<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