XSLT - 提取名称 - 值对 [英] XSLT - Extracting name-value pairs
问题描述
假设我在XML文件中有一些节点,其URL属性:
< 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" />
等等。
假设我想要检索这个@url参数,但只能用
值,在查询字符串中,与foo相关联和foo2 (因此
丢弃name1,name2,name4和其他所有不同的东西)。
换句话说,我必须获得:
>
mypage.php?foo = bar& foo2 = bar2
myotherpage.php?& foo = bar3& foo2 = bar5
......和等等。
在使用XSL进行转换时,是否有一种方便的方法来获取这个
字符串操作? (如果可能的话,我更愿意坚持使用XSLT1.0)
提前感谢您的帮助。
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.
推荐答案
Ebenezer写道:
Ebenezer wrote:
假设我在XML文件中有一些节点,其URL属性:
< 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" />
等等。
假设我想要检索这个@url参数,但只能用
值,在查询字符串中,与foo相关联和foo2 (因此
丢弃name1,name2,name4和其他所有不同的东西)。
换句话说,我必须获得:
>
mypage.php?foo = bar& foo2 = bar2
myotherpage.php?& foo = bar3& foo2 = bar5
......等等on。
在使用XSL进行转换时,是否有一种方便的方法来获取这个
字符串操作? (我更愿意坚持使用XSLT1.0,如果可能的话)
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)
好
substring-before(node / @ url, ''''')
会给你文件名,需要解析查询字符串
需要一个递归模板或XSLT 1.0中的扩展函数。
在XSLT 2.0中你可以使用tokenize函数和/或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(
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(
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(
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(
这篇关于XSLT - 提取名称 - 值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!