XSLT3 用分隔符连接值 [英] XSLT3 joining values with separator

查看:31
本文介绍了XSLT3 用分隔符连接值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 XSLT 还很陌生,我一直在努力复制这里提到的解决方案XSL for-each:如何检测最后一个节点?比我愿意承认的时间更长:(

I'm pretty new to XSLT and I've been struggling to replicate the solution mentioned here XSL for-each: how to detect last node? for longer than I'm willing to admit :(

我已经设置了这个小提琴.https://xsltfiddle.liberty-development.net/naZXVFi

I've setup this fiddle. https://xsltfiddle.liberty-development.net/naZXVFi

我希望我可以只使用 value-of + 分隔符,而不是选择/when xslt 工具,因为它看起来更惯用.

I was hoping I could use just the value-of + separator, vs choose / when xslt tools, as it did seem more idiomatic.

  • 我无法显示分隔符;
  • 我也不能只选择技能的孩子,我总是也会得到后代.也就是说,我不应该在输出中看到任何 detail.
  • 奖励:不知道为什么那个元标记没有自动关闭(在 html 部分中发出警告)

所需的输出:skill1、skill2、skill3、skill4、skill5(最后一个没有逗号)

Desired output: skill1, skill2, skill3, skill4, skill5 (no comma space for the last one)

任何帮助将不胜感激.谢谢.

Any help would be greatly appreciated. Thanks.

也包括此处的代码:xml: (需要给 xslt 添加 ref):

including the code here too: xml: (need to add ref to xslt):

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?> <!-- not in fiddle -->

<skills>
    <skill>skill1</skill>
    <skill>skill2</skill>
    <skill>skill3
        <details>
            <detail>detail1</detail>
            <detail>detail2</detail>
        </details>
    </skill>
    <skill>skill4</skill>
    <skill>skill5</skill>
</skills>

test.xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:map="http://www.w3.org/2005/xpath-functions/map"
    xmlns:array="http://www.w3.org/2005/xpath-functions/array"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output method="html" indent="yes" html-version="5"/>

  <xsl:template match="/">
    <html>
      <head>
        <title>.NET XSLT Fiddle Example</title>
      </head>
      <body>
         <xsl:for-each select="/skills/skill">
            <xsl:value-of select="." separator=", "/>
        </xsl:for-each>
      </body>
    </html>


  </xsl:template>

</xsl:stylesheet>

推荐答案

一般来说,使用 XSLT 2/3 来输出由一些分隔符字符串分隔的序列,您只需使用 xsl:value-of select="$sequence"separator 属性中带有适当的分隔符字符串(并且没有 for-each):

In general, with XSLT 2/3 to output a sequence separated by some separator string, you simply use xsl:value-of select="$sequence" with the appropriate separator string in the separator attribute (and no for-each):

  <xsl:template match="skills">
    <xsl:value-of select="skill/text()[normalize-space()]/normalize-space()" separator=", "/>
  </xsl:template>

https://xsltfiddle.liberty-development.net/naZXVFi/1

在大多数情况下,您只需要 select="skill" separator=", " 但考虑到您的后代和空白,您似乎想要消除 select上面的表达式有点复杂.

In most cases you would just need select="skill" separator=", " but given your descendants and the white space you seem to want to eliminate the select expression above is a bit more complicated.

这篇关于XSLT3 用分隔符连接值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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