Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - XSLT - Extracting name-value pairs >Thread Next - Re: XSLT - Extracting name-value pairs Re: XSLT - Extracting name-value pairsTo: 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, '&')"> <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, '&')"/> </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/ | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
