使用模板将元素的值连接到变量? [英] Concatenate values of elements to a variable with the usage of a template?

查看:18
本文介绍了使用模板将元素的值连接到变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下数据需要拼接.但是我收到的 XML 文档可以有零到 n"个 b 元素.换句话说,如果没有 b 元素,则 xslt 仍应正常工作,例如:

The following data need to be concatenated. But the XML document which I received can have "zero to n" b elements. In other words if there are no b elements the xslt should still work correctly example:

 <a>
   <b1>Some</b2>
   <b2>data</b2>
   <b3>what</b3>
   <b4>need</b4>
   <b5>to</b5>
   <b6>be</b6>
   <b7>concatenated</b7>
</a>

预期结果

<a>
  <b1>Some data what need to be concatenated</b1>
</a>

我正在尝试以下结构,但无法使其正常工作.

I was trying the following construction but I couldn't made it work.

<xsl:variable name="details" select="//b*"/>
<xsl:for-each select="$details">
    <!-- how can I concatenate the values of the b's to a variable????-->
</xsl:for-each>
 <!-- Process the variable for further needs-->

我希望有人能给我一个提示吗?问候德克

I hope some body can give me a hint? Regards Dirk

推荐答案

您不能使用//b* 来选择所有以 b 开头的元素,因为 XPath 总是在不使用通配符的情况下进行精确匹配(也许命名空间除外).所以你需要使用//*[starts-with(name(), "b")] 来选择 b 个元素

You cannot use //b* to select all elements starting with b, since XPath is always doing an exact matching without wildcards (perhaps except for namespaces). So you need to use //*[starts-with(name(), "b")] to select the b elements

然后你可以在 XPath 中单独使用 string-join 函数进行连接:

Then you can do the concatenation is in XPath alone with the string-join function:

string-join(//*[starts-with(name(), "b")]/text(), " ")

这篇关于使用模板将元素的值连接到变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆