如何在 <xs:list> 中找到元素的数量?使用 XSLT? [英] How do I find the number of elements in a &lt;xs:list&gt; using XSLT?

查看:68
本文介绍了如何在 <xs:list> 中找到元素的数量?使用 XSLT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下类型的 XML 架构:

I have an XML schema that contains the following type :

<xs:simpleType name="valuelist">
  <xs:list itemType="xs:double"/>
</xs:simpleType>

示例 XML 片段将是:

A sample XML fragment would be:

<values>1 2 3.2 5.6</values>

在 XSLT 转换中,如何获取列表中的元素数量?

In an XSLT transform, how do I get the number of elements in the list?

如何遍历元素?

推荐答案

I.XPath 2.0 (XSLT 2.0) 解决方案:

count(tokenize(., ' '))

二.XPath 1.0 (XSLT 1.0) 解决方案:

 string-length()
-
 string-length(translate(normalize-space(), ' ', ''))
+ 1

至于对这个列表的项目进行迭代:

As for iteration over the items of this list:

  1. 在 XPath 2.0/XSLT 2.0 中,只需使用上面的 XPath 2.0 表达式作为 select 属性的值:
  1. In XPath 2.0 / XSLT 2.0 just use the above XPath 2.0 expression as the value of a select attribute:

--

 for $i in tokenize(., ' '),
     $n in number($i)
  return
    yourXPathExpression

--

2. 在 XSLT 1.0 中,您需要有更多代码用于拆分/标记化.这个问题有几个很好的答案(其中一部分是我的)——只需搜索xslt split a string"之类的内容

2. In XSLT 1.0 you need to have some more code for splitting/tokenization. There are several good answers to this question (part of them mine) -- just search for something like "xslt split a string"

这篇关于如何在 <xs:list> 中找到元素的数量?使用 XSLT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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