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

前端之家收集整理的这篇文章主要介绍了xml – 如何使用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的更多信息.

猜你在找的XML相关文章