Retrieve page URL params or page URL in XSLT

前端 未结 4 1281
不知归路
不知归路 2021-01-14 13:37

I have a page which has URL structure : http://www.abc.com/xyz?parama=1¶mb=2

  • Is is possible to create a generic method for getting the values of an

相关标签:
4条回答
  • 2021-01-14 14:02

    This might be possible using EXSLT, or registering a function callback with your particular xslt processor, but not with plain old XSLT, at least not to my knowledge. With plain XSLT, if it isn't in the XML, then it doesn't exist.

    0 讨论(0)
  • 2021-01-14 14:03

    If it is client side, there isn't any way of doing this. Unfortunately, xsl is very limited in the browser. I'm afraid you will need to move the functionality to the webapp or to javascript.

    If the transform is server side, there might be something you can do.

    0 讨论(0)
  • 2021-01-14 14:14

    I workaround this by using javascript in mi XSLT file. take a look.

    on The XML I have a tag element named tag! yes, very original...

    <tag url="http://www.demo.net/share.php?u=param1 />
    

    Mi Sample transform

    <div class="HelloDiv">
            <xsl:for-each select="tag">
              <a href="{@url}">
                This is my custom URL
              </a>
            </xsl:for-each>
    </div>
    

    Now inside the template of the transform, in going to load my custom value in Param1 in this case i'm going to use the document.title.. by using a jquery function.

     <script type="text/javascript">
          jQuery(function(){
            jQuery('div.HelloDiv a').each(function(){
              var parameter = jQuery(this).attr('href');
              parameter = currentUrl.replace('param1',escape(document.title));
              jQuery(this).attr('href',parameter);
            })
          });
      </script>
    
    0 讨论(0)
  • 2021-01-14 14:19

    Is is possible to get the URL of the page in XSL similar to javascript's location.href ?

    Not exactly in the same way, but yes, the query string can be passed as a parameter.

    Is is possible to create a generic method for getting the values of any additional params the URL maybe (parama=1&paramb=2)

    Yes, one can perform tokenization (with the tokenize() function in XSLT 2.0 or in XSLT 1.0 using the str-split-to-words template of **FXSL 1.x or a self-written recursive tokenization template.)

    XSLT 1.0 solution:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:ext="http://exslt.org/common"
    >
    
       <xsl:import href="strSplit-to-Words.xsl"/>
    
       <xsl:output indent="yes" omit-xml-declaration="yes"/>
         <xsl:param name="pQString" select=
         "'?parama=1&amp;paramb=2&amp;anyParamName=AnyValue'"
         />
    
    
        <xsl:template match="/">
        <xsl:variable name="vwordNodes">
          <xsl:call-template name="str-split-to-words">
            <xsl:with-param name="pStr" select="$pQString"/>
            <xsl:with-param name="pDelimiters"
                      select="'?&amp;'"/>
          </xsl:call-template>
        </xsl:variable>
    
        <xsl:apply-templates select=
         "ext:node-set($vwordNodes)/*
         "/>
        </xsl:template>
    
        <xsl:template match="word">
          <xsl:value-of select="."/>
          <xsl:text>&#xA;</xsl:text>
        </xsl:template>
    </xsl:stylesheet>
    

    when the above transformation is applied on any XML document (will not be used), the wanted result is produced:

    parama=1
    paramb=2
    anyParamName=AnyValue
    

    Do note the use of the FXSL 1.x str-split-to-words template and the use of the EXSLT ext:node-set() extension function.

    XSLT 2.0 solution:

     <xsl:param name="pQString" as="xs:string" select=
     "'?parama=1&amp;paramb=2&amp;anyParamName=AnyValue'"
     />
    
    <xsl:template match="/">
      <xsl:value-of separator="&#xA;" select=
      "tokenize($pQString, '\?|&amp;')
      "/>
    </xsl:template>
    

    When the above XSLT 2.0 transformation is performed, it produces the correct result:

    parama=1
    paramb=2
    anyParamName=AnyValue
    
    0 讨论(0)
提交回复
热议问题