XSLT 属性中的最后一个位置
我需要检查具有特定属性的节点是否是循环中的最后一个,并相应地在最后一个节点之前插入和符号。我尝试使用
<xsl:if test="position() = last()">
似乎没有考虑属性值。
XML
<creators and-others="no">
<creator type="personal">Roche, R.L.</creator>
<creator type="personal">Moulin, D.</creator>
<creator type="personal">James, K.</creator>
<creator type="affiliation">CEA Centre d'Etudes Nucleaires de Saclay, 91 - Gif-sur- zYvette (France). Dept. d'Etudes Mecaniques et Thermiques</creator>
</creators>
这是我需要输出的 ,如:Roche, RL, Moulin, D., & James, K.
另外,有没有办法获取属性为“personal”的“creator”节点的值计数?
I need to check whether a node with certain attribute is the last in the loop, and accordingly insert and ampersand before the last node. I tried using the
<xsl:if test="position() = last()">
doesn't seem to take into consideration the attribute value.
Here's the XML
<creators and-others="no">
<creator type="personal">Roche, R.L.</creator>
<creator type="personal">Moulin, D.</creator>
<creator type="personal">James, K.</creator>
<creator type="affiliation">CEA Centre d'Etudes Nucleaires de Saclay, 91 - Gif-sur- zYvette (France). Dept. d'Etudes Mecaniques et Thermiques</creator>
</creators>
I need the output as, Roche, R.L., Moulin, D., & James, K.
Also, is there a way, to get the count of values for the "creator" node, with attribute "personal" ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为什么不计算匹配模板中的以下兄弟姐妹?同级节点之后的最后一个计数
creator[@type='personal']
节点将为零。XSLT 2.0 在 Saxon-HE 9.2.1.1J 下测试(也在 MSXSL 4.0 上作为 XSLT 1.0
工作 )根据您的输入,产生:
根据模板上下文,您始终可以使用 axis 来获取节点的计数。示例:
将
$count
设置为3
。Why not counting the followng siblings inside the matching template? The last count
creator[@type='personal']
node following sibling will be zero.XSLT 2.0 tested under Saxon-HE 9.2.1.1J (also wroking as XSLT 1.0 on MSXSL 4.0)
Applied on your input, produces:
According to the template context, you can always use axis to get the count of the nodes. Example:
will set
$count
to3
.您可以尝试迭代
并使用您的
那里。 XPath 应选择所有这些元素,其中属性type
的值为personal
。您可以在
count
函数中使用类似的表达式。You can try iterating over
<xsl:for-each select="creator[@type='personal']">
and use your<xsl:if>
there. The XPath should select all those elements, for which attributetype
has the value ofpersonal
.You can use a similar expression in the
count
function.这应该可行
如果您想将其存储在变量中,
This should work
If you want to store it in a variable
使用谓词
[@type='personal']
仅选择所需的
元素。然后,您的测试条件无需使用任何计数或引用同级元素即可工作。您还应该测试是否只有一个
,否则您最终会输出一个额外的&
或逗号。因此,
在这里比单独的
更方便。下面的示例代码。您还可以使用
而不是为creator
使用单独的模板并调用
如果您想在现有模板中执行此操作。给定输入,
它会产生以下输出
Use a predicate
[@type='personal']
to select only the desired<creator>
elements. Then your test condition works without using any counts or referring to sibling elements. You should also test if there is only one<creator type="personal">
, otherwise you'll end up outputting an extra&
or a comma. Therefore<xsl:choose>
is here handier than<xsl:if>
alone.Sample code below. You could also use
<xsl:for-each select="creator[@type='personal']">
instead of having a separate template forcreator
and calling<xsl:apply-templates select="creator[@type='personal']"/>
if you want to do this inside an existing template.Given input
it produces the following output
这是一个更短的解决方案,根本不使用任何 XSLT 条件指令:
当应用于提供的 XML 文档时:
生成所需的正确结果:
使用:
说明:
使用模式匹配代替 XSLT 条件指令。
利用 XSLT(默认)处理模型和内置模板。
Here is a shorter solution that doesn't use any XSLT conditional instructions at all:
when applied on the provided XML document:
the wanted, correct result is produced:
Use:
Explanation:
Use of pattern matching instead of XSLT conditional instructions.
Making use of the XSLT (default) processing model and built-in templates.