Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: XSLT - Extracting name-value pairs

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 11/7/2008 6:49:00 PM
Ebenezer wrote:
> Let's suppose I have some nodes in an XML file, with an URL attribute:
> 
> <node url="mypage.php?name1=value1&foo=bar&foo2=bar2&name2=value0" />
> <node url="myotherpage.php?name4=value4&foo=bar3&foo2=bar5&name2=value8" />
> 
> and so on.
> 
> Let's suppose I want to retrieve this @url parameter, BUT ONLY with the 
> values, in querystring, associated with "foo" and "foo2" (thus 
> discarding name1, name2, name4 and every other different ones).
> 
> In other words, I must obtain:
> 
> mypage.php?foo=bar&foo2=bar2
> myotherpage.php?&foo=bar3&foo2=bar5
> ... and so on.
> 
> Is there a convenient way, in a transformation with XSL, to obtain this 
> string manipulation? (I'd prefer to stick to XSLT1.0, if possible)

Well
   substring-before(node/@url, '?')
would give you the file name, the query string would need to be parsed 
which needs a recursive template or an extension function in XSLT 1.0. 
In XSLT 2.0 you could use the tokenize function and/or xsl:analyze-string:

<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:mf="http://example.com/2008/mf"
   exclude-result-prefixes="xsd mf"
   version="2.0">

   <xsl:function name="mf:get-query" as="xsd:string">
     <xsl:param name="qs" as="xsd:string"/>
     <xsl:param name="params" as="xsd:string*"/>
     <xsl:variable name="filtered-qs" as="xsd:string*">
       <xsl:for-each select="tokenize($qs, '&amp;')">
         <xsl:analyze-string
           select="."
           regex="({string-join($params, '|')})=\w*">
           <xsl:matching-substring>
             <xsl:sequence select="regex-group(0)"/>
           </xsl:matching-substring>
         </xsl:analyze-string>
       </xsl:for-each>
     </xsl:variable>
     <xsl:sequence select="string-join($filtered-qs, '&amp;')"/>
   </xsl:function>

   <xsl:template match="node">
     <xsl:value-of select="concat(substring-before(@url, '?'), '?', 
mf:get-query(substring-after(@url, '?'), ('foo', 'foo2')))"/>
   </xsl:template>

</xsl:stylesheet>


-- 

	Martin Honnen
	http://JavaScript.FAQTs.com/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent