Passing a URL variable into xsl

前端 未结 2 1090
春和景丽
春和景丽 2021-01-19 01:25

Is it possible to pass a URL variable into xsl.

EG. http:www.somedomain.com/index.aspx?myVar=test&myVar2=anotherTest

I\'d like to be able to use t

相关标签:
2条回答
  • 2021-01-19 01:52

    Here is a more generic transformation that obtains any number of query string parameters from a given URL:

    <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:param name="pUrl" select=
     "'http:www.somedomain.com/index.aspx?myVar=test&amp;myVar2=anotherTest&amp;myVar3=yetAnotherTest'"/>
    
     <xsl:template match="/">
         <xsl:call-template name="GetQueryStringParams"/>
     </xsl:template>
    
     <xsl:template name="GetQueryStringParams">
      <xsl:param name="pUrl" select="$pUrl"/>
    
          <xsl:variable name="vQueryPart" select=
          "substring-before(substring-after(concat($pUrl,'?'),
                                            '?'),
                          '?')"/>
    
          <xsl:variable name="vHeadVar" select=
           "substring-before(concat($vQueryPart,'&amp;'), '&amp;')"/>
    
           <xsl:element name="{substring-before($vHeadVar, '=')}">
             <xsl:value-of select="substring-after($vHeadVar, '=')"/>
           </xsl:element>
    
        <xsl:variable name="vRest" select="substring-after($vQueryPart, '&amp;')"/>
    
        <xsl:if test="string-length($vRest) > 0">
           <xsl:call-template name="GetQueryStringParams">
             <xsl:with-param name="pUrl" select=
             "concat('?', substring(substring-after($vQueryPart, $vHeadVar), 2))"/>
           </xsl:call-template>
        </xsl:if>
     </xsl:template>
    </xsl:stylesheet>
    

    when this transformation is applied on any XML document (not used), the wanted, correct result is produced:

    <myVar>test</myVar>
    <myVar2>anotherTest</myVar2>
    <myVar3>yetAnotherTest</myVar3>
    
    0 讨论(0)
  • 2021-01-19 02:02

    Sure, you can. Use xsl:param element in xsl:stylesheet element and pass parameter from your XSL engine.

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:param name="url"/>
    

    Then use string-functions, e.g.:

    <xsl:variable name="right-part" select="substring-after($url, 'myVar=')"/>
    <xsl:value-of select="substring-before(substring-before($right-part, 'myVar2='), '&amp;')"/>
    <xsl:text>|</xsl:text>
    <xsl:value-of select="substring-after($right-part, 'myVar2=')"/>
    
    0 讨论(0)
提交回复
热议问题