Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 

Go Back   XSL - XML - RSS Forums > XSLForum: Main > XSL-XSD-XML and more

Tags:



Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-07-2008, 05:36 PM
Ebenezer
 
Posts: n/a

Default XSLT - Extracting name-value pairs



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&nam e2=value0" />
<node url="myotherpage.php?name4=value4&foo=bar3&foo2=ba r5&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.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-07-2008, 06:23 PM
Martin Honnen
 
Posts: n/a

Default Re: XSLT - Extracting name-value pairs

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&nam e2=value0" />
> <node url="myotherpage.php?name4=value4&foo=bar3&foo2=ba r5&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">
<xslaram name="qs" as="xsd:string"/>
<xslaram 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/
Reply With Quote
  #3 (permalink)  
Old 11-08-2008, 04:57 PM
Dimitre Novatchev
 
Posts: n/a

Default Re: XSLT - Extracting name-value pairs


"Ebenezer" <vaciapairat@spam.com> wrote in message
news:5j_Qk.187764$FR.474551@twister1.libero.it...
> 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&nam e2=value0" />
> <node url="myotherpage.php?name4=value4&foo=bar3&foo2=ba r5&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 -->

<xslutput 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">
<xslaram name="pInput"/>
<xslaram 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="'&amp;'"/>
</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">&amp;</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&amp;foo=bar&amp;foo2= bar2&amp;name2=value0" />
<node
url="myotherpage.php?name4=value4&amp;foo=bar3&amp ;foo2=bar5&amp;name2=value8"
/>
</nodes>

produces the wanted results:

<nodes>
<node url="mypage.php?foo=bar&amp;foo2=bar2"/>
<node url="myotherpage.php?foo=bar3&amp;foo2=bar5"/>
</nodes>

Hope this helped.

Cheers,
Dimitre Novatchev


Reply With Quote
  #4 (permalink)  
Old 11-08-2008, 06:44 PM
Ebenezer
 
Posts: n/a

Default Re: XSLT - Extracting name-value pairs

Johannes Koch ha scritto:
> Ebenezer schrieb:
>> 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&nam e2=value0" />
>> <node
>> url="myotherpage.php?name4=value4&foo=bar3&foo2=ba r5&name2=value8" />

>
> If you have this, it's not XML. Instead of '&' in the url attribute
> value, write '&amp;'.


Of course, but the parser I use won't bother on that.
Reply With Quote
  #5 (permalink)  
Old 11-08-2008, 06:44 PM
Ebenezer
 
Posts: n/a

Default Re: XSLT - Extracting name-value pairs

Dimitre Novatchev ha scritto:
> "Ebenezer" <vaciapairat@spam.com> wrote in message
> news:5j_Qk.187764$FR.474551@twister1.libero.it...
> Using FXSL 1.x and its "str-split-to-words" template, such processing is
> trivial.


Unfortunately, I use another engine.
Reply With Quote
  #6 (permalink)  
Old 11-09-2008, 05:07 AM
Dimitre Novatchev
 
Posts: n/a

Default Re: XSLT - Extracting name-value pairs

>> Unfortunately, I use another engine.
>
> Hmm... Did I say that a specific "engine" was necessary for the solution?
>


Or did you think that FXSL was an "engine"?

It most certainly isn't an "engine".

FXSL is a library of functions/templates, written in pure XSLT. As such, its
functions can be used under any compliant XSLT 2.0 processor or any
compliant XSLT 1.0 processor that either implements the exsl:node-set()
extension function, or is one of the following XSLT 1.0 processors: MSXML
(3,4,6), Xalan, Saxon.

Cheers,
Dimitre Novatchev

"Dimitre Novatchev" <dnovatchev@cnntp.org> wrote in message
news:49165fc4$0$17067$6e1ede2f@read.cnntp.org...
>
> "Ebenezer" <vaciapairat@spam.com> wrote in message
> news:AAkRk.188514$FR.475337@twister1.libero.it...
>> Dimitre Novatchev ha scritto:
>>> "Ebenezer" <vaciapairat@spam.com> wrote in message
>>> news:5j_Qk.187764$FR.474551@twister1.libero.it...
>>> Using FXSL 1.x and its "str-split-to-words" template, such processing is
>>> trivial.

>>
>> Unfortunately, I use another engine.

>
> Hmm... Did I say that a specific "engine" was necessary for the solution?
>
> FXSL 1.x works with *any* XSLT 1.0 processor that supports the
> exsl:node-set() extension function.
>
> There are 3 separate versions for MSXML, Xalan and Saxon, although the
> last two XSLT 1.0 processors implement exsl:node-set().
>
> For MSXML, there is a way to implement exslt:node-set() (and in any XSLT
> processor that supports extension functions written in inline-JavaScript).
> See:
>
> http://www.jenitennison.com/blog/node/24
>
> Lastly, FXSL 2.0 is the latest (since the last 4-5 years) version of FXSL
> which is written in and for XSLT 2.0. It doesn't use any extension
> functions at all.
>
>



Reply With Quote
  #7 (permalink)  
Old 11-10-2008, 09:46 AM
Ebenezer
 
Posts: n/a

Default Re: XSLT - Extracting name-value pairs

Dimitre Novatchev ha scritto:
> "Ebenezer" <vaciapairat@spam.com> wrote in message
> news:4AkRk.188513$FR.475325@twister1.libero.it...
>> Johannes Koch ha scritto:
>>> Ebenezer schrieb:
>>>> 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&nam e2=value0" />
>>>> <node url="myotherpage.php?name4=value4&foo=bar3&foo2=ba r5&name2=value8"
>>>> />
>>> If you have this, it's not XML. Instead of '&' in the url attribute
>>> value, write '&amp;'.

>> Of course, but the parser I use won't bother on that.

> Then this is not a true (compliant) XML parser.


You got it
Reply With Quote
  #8 (permalink)  
Old 11-10-2008, 09:46 AM
Ebenezer
 
Posts: n/a

Default Re: XSLT - Extracting name-value pairs

Dimitre Novatchev ha scritto:
>>> Unfortunately, I use another engine.

>> Hmm... Did I say that a specific "engine" was necessary for the solution?
>>

>
> Or did you think that FXSL was an "engine"?
> It most certainly isn't an "engine".
> FXSL is a library of functions/templates, written in pure XSLT. As such, its
> functions can be used under any compliant XSLT 2.0 processor or any
> compliant XSLT 1.0 processor that either implements the exsl:node-set()
> extension function, or is one of the following XSLT 1.0 processors: MSXML
> (3,4,6), Xalan, Saxon.


Yes, actually I thought that it was an engine, never heard of this library.
Lot of thanks for sharing and helping, Dimitre, I'll dig into that too.
Cheers.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Contact Us -|- XSL - XML - RSS Forums -|- Archive -|- Top -|-Rules/Disclaimer-|-Help/Support -|-Advertise
© Camley Interactive (camley.info) 2008 - all logos and images are copywrite their respective owners.
Proud member of the Camley Interactive Network
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
All times are GMT. The time now is 04:46 PM.
Style Developed by Epic Designz