XSLT&字符串操作查询 [英] XSLT & String manipulation query
问题描述
您好,
我正在尝试在我的样式表中执行一些字符串操作,并且
已经陷入了下面的问题,所以希望可以引出一些有用的
提示。
即问题是我需要在身份转换中将不合格的Xpath转换为
a完全限定的Xpath,即
/ AAA / BBB / CCC / @ DDD
转换为
/ ns :AAA / ns:BBB / ns:CCC / @ DDD
带有预定义的NS前缀并使用字符串标记器(从
http://www.xslt.com/html/xsl-list/2005- 04 / msg00031.html )返回
代币为:
< token> AAA< / token>
< token> BBB< / token>
< token> CCC< / token>
< token> @ DDD< / token>
我正在分配变量''令牌' '在下面的模板中,
然后尝试生成完全限定的命名空间:
< xsl:template name =" qualifiedXpath">
< xsl:param name =" unqualifiedXpath" />
<! - - >
< xsl:变量名=" sampleUnqualifiedXpath"
select ="''/ AAA / BBB / CCC / @ DDD''" />
<! - 硬编码名称空间前缀 - >
< xsl:variable name =" prefixString" select ="''dns:''" />
<! - 硬编码分隔符 - >
< xsl:variable name = "斜线" select ="''/''" />
<! - 包含令牌的变量 - >
< xsl:变量名=" tokens">
<! - 调用令牌化器模板 - >
< xsl:call-template name =" tokenizer">
< xsl:with-param name =" string" select =" $ sampleUnqualifiedXpath" />
< xsl:with-param name =" delimiter" select =" $ slash" />
< / xsl:call-template>
< / xsl:variable>
< ;! - 变量以保存合格的Xpath - >
< xsl:variable name =" qualXpath">
<! - 构建合格的Xpath - >
<! - 迭代返回的令牌节点 - >
< xsl:for-each select =" $令牌/令牌">
<! - 添加分隔符 - >
< xsl:value-of select =" $ slash" />
<! - 只有当token是元素名称时才添加前缀,即没有
@字符 - >
< xsl:if test =" not(contains(。,''@''))">
<! - 添加名称空间前缀 - >
< xsl:value-of select =" $ prefixString" />
<! - 添加令牌的价值 - >
< xsl:value-of select ="。" />
< / xsl:if>
< / xsl:for-每个>
&l t; / xsl:variable>
<! - 返回qualXpath变量 - >
< xsl:value-of select =" $ qualXpath" / >
< / xsl:template>
其中令牌在循环声明中的用法
< xsl :for-each select =" $ tokens / token">
导致结果树片段(RTF)错误;我已经谷歌搜索了更多关于它的信息,但坦率地说不明白这个问题,并且不确定
是另一种解决方案,因此寻求智慧长老们!
:-)
BTW,上面的代码片段可能是错误的(希望不会太多),因为
我一直坚持这个RTF错误,因此无法继续。我怀疑
使用contains(。,''@''),
我打算检查令牌节点''@'的值'性格,是
有点可疑。 :)
非常感谢!
问候,
Bilal B.
***通过开发人员指南发送 http://www.developersdex.com ***
Hello,
I''m trying to perform some string manipulations in my stylesheet and
have gotten stuck on the issue below so hopefully can elicit some useful
hints.
Namely, the problem is that I need to convert an unqualified Xpath to
a fully qualified Xpath in an identity transform, i.e.
/AAA/BBB/CCC/@DDD
converted to
/ns:AAA/ns:BBB/ns:CCC/@DDD
with a predefined NS prefix and using a string tokenizer (adopted from
http://www.xslt.com/html/xsl-list/2005-04/msg00031.html) which returns
the tokens as:
<token>AAA</token>
<token>BBB</token>
<token>CCC</token>
<token>@DDD</token>
I''m assigning to the variable ''tokens'' in the following template, which
then tries to produce the fully qualified namespace:
<xsl:template name="qualifiedXpath">
<xsl:param name="unqualifiedXpath"/>
<!-- -->
<xsl:variable name="sampleUnqualifiedXpath"
select="''/AAA/BBB/CCC/@DDD''"/>
<!-- hardcoded namespace prefix -->
<xsl:variable name="prefixString" select="''dns:''"/>
<!-- hardcoded delimiter character -->
<xsl:variable name="slash" select="''/''"/>
<!-- Variable to contain the tokens -->
<xsl:variable name="tokens">
<!-- Calling tokenizer template-->
<xsl:call-template name="tokenizer">
<xsl:with-param name="string" select="$sampleUnqualifiedXpath"/>
<xsl:with-param name="delimiter" select="$slash"/>
</xsl:call-template>
</xsl:variable>
<!-- Variable to hold the qualified Xpath -->
<xsl:variable name="qualXpath">
<!-- Constructing the qualified Xpath-->
<!-- Iterate through the returned token nodes -->
<xsl:for-each select="$tokens/token">
<!-- Add delimiter-->
<xsl:value-of select="$slash"/>
<!-- Add prefix only when token is an element name i.e. doesn''t have the
@ character -->
<xsl:if test="not(contains(.,''@''))">
<!-- Adding namespace prefix -->
<xsl:value-of select="$prefixString"/>
<!-- Add token''s value -->
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<!-- returning qualXpath variable -->
<xsl:value-of select="$qualXpath"/>
</xsl:template>
where the tokens'' usage in the loop declaration
<xsl:for-each select="$tokens/token">
is causing a Result Tree Fragment (RTF) error; I''ve googled to find out
more about it but frankly don''t understand the problem, and unsure what
an alternative solution would be and hence seeking Wisdom of The Elders!
:-)
BTW, the code snippet above might be buggy (hopefully not too much) as
I''ve been stuck at this RTF error and hence unable to proceed. I suspect
the usage of contains(.,''@''),
where I intend to check the token node''s value for a ''@'' character, is
bit fishy. :)
Many thanks!
Regards,
Bilal B.
*** Sent via Developersdex http://www.developersdex.com ***
推荐答案
sampleUnqualifiedXpath" />
< xsl:with-param name =" ;定界符" select ="
sampleUnqualifiedXpath"/>
<xsl:with-param name="delimiter" select="
slash" />
< / xsl:call-template>
< / xsl:变量>
<! - 用于保存合格Xpath的变量 - >
< xsl:variable name =" qualXpath">
<! - 构建合格的Xpath - >
<! - 迭代返回的令牌节点 - >
< xsl:for-each select ="
slash"/>
</xsl:call-template>
</xsl:variable>
<!-- Variable to hold the qualified Xpath -->
<xsl:variable name="qualXpath">
<!-- Constructing the qualified Xpath-->
<!-- Iterate through the returned token nodes -->
<xsl:for-each select="
tokens / token">
<! - Add delimiter - >
< xsl:value-of select ="
tokens/token">
<!-- Add delimiter-->
<xsl:value-of select="
这篇关于XSLT&字符串操作查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!