加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – 如何使用XSLT 1.0从字符串中提取唯一字符?

发布时间:2020-12-16 23:08:22 所属栏目:百科 来源:网络整理
导读:我在XSLT设计中面临的最艰巨的挑战之一.. 如何复制给定字符串中的唯一字符.. 测试xml是: rootstringaaeerstrst11232434/string/root 我期待的输出是: stringaerst1234/string 解决方法 这是一个XSLT 1.0解决方案,比当前选择的答案更短,更易于编写,因为它使
我在XSLT设计中面临的最艰巨的挑战之一..

如何复制给定字符串中的唯一字符..
测试xml是:

<root>
<string>aaeerstrst11232434</string>
</root>

我期待的输出是:

<string>aerst1234</string>

解决方法

这是一个XSLT 1.0解决方案,比当前选择的答案更短,更易于编写,因为它使用了 FXSL的str-foldl模板.

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:f="http://fxsl.sf.net/"
 exclude-result-prefixes="f">

 <xsl:import href="str-foldl.xsl"/>
 <xsl:output method="text"/>

 <f:addUnique/>

 <xsl:variable name="vFunAddunique" select=
  "document('')/*/f:addUnique[1]
  "/>

    <xsl:template match="string">
      <xsl:call-template name="str-foldl">
        <xsl:with-param name="pFunc" select="$vFunAddunique"/>
        <xsl:with-param name="pA0" select="''"/>
        <xsl:with-param name="pStr" select="."/>
      </xsl:call-template>
    </xsl:template>

    <xsl:template match="f:addUnique" mode="f:FXSL">
      <xsl:param name="arg1"/>
      <xsl:param name="arg2"/>

      <xsl:value-of select="$arg1"/>
      <xsl:if test="not(contains($arg1,$arg2))">
       <xsl:value-of select="$arg2"/>
      </xsl:if>
    </xsl:template>
</xsl:stylesheet>

将上述转换应用于最初提供的源XML文档时:

<root>
    <string>aaeerstrst11232434</string>
</root>

产生了想要的结果:

aerst1234

阅读有关FXSL 1.x(适用于XSLT 1.0)here以及FXSL 2.x(适用于XSLT 2.0)here的更多信息.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读