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/8/2008 8:41:00 AM "Ebenezer" <vaciapairat@s...> wrote in message news:5j_Qk.187764$FR.474551@t...... > 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) > > Thanks in advance for your help. Using FXSL 1.x and its "str-split-to-words" template, such processing is trivial. This transformation: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext" > <xsl:import href="strSplit-to-Words.xsl"/> <!-- To be applied upon: testTokenize2.xml --> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="@url"> <xsl:attribute name="{name()}"> <xsl:variable name="vHdUrl" select= "substring-before(.,'?')" /> <xsl:variable name="vTlUrl" select= "substring-after(.,'?')" /> <xsl:variable name="vFilteredActions"> <xsl:call-template name="filterActions"> <xsl:with-param name="pInput" select="$vTlUrl"/> <xsl:with-param name="pMustStart" select="'foo'"/> </xsl:call-template> </xsl:variable> <xsl:value-of select= "concat($vHdUrl,'?',$vFilteredActions)" /> </xsl:attribute> </xsl:template> <xsl:template name="filterActions"> <xsl:param name="pInput"/> <xsl:param name="pMustStart" select="'x'"/> <xsl:variable name="vTokens"> <xsl:call-template name="str-split-to-words"> <xsl:with-param name="pStr" select="$pInput"/> <xsl:with-param name="pDelimiters" select="'&'"/> </xsl:call-template> </xsl:variable> <xsl:for-each select= "ext:node-set($vTokens)/word [starts-with(.,$pMustStart)]" > <xsl:variable name="vactDelim"> <xsl:if test="position() > 1">&</xsl:if> </xsl:variable> <xsl:value-of select="concat($vactDelim, .)"/> </xsl:for-each> </xsl:template> </xsl:stylesheet> when applied on this xml document: <nodes> <node url="mypage.php?name1=value1&foo=bar&foo2=bar2&name2=value0" /> <node url="myotherpage.php?name4=value4&foo=bar3&foo2=bar5&name2=value8" /> </nodes> produces the wanted results: <nodes> <node url="mypage.php?foo=bar&foo2=bar2"/> <node url="myotherpage.php?foo=bar3&foo2=bar5"/> </nodes> Hope this helped. Cheers, Dimitre Novatchev | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
