XSLT 設計でこれまでに直面した中で最も困難な課題の 1 つ..
特定の文字列の一意の文字をコピーする方法..
テストxmlは次のとおりです。
<root>
<string>aaeerstrst11232434</string>
</root>
私が期待している出力は次のとおりです。
<string>aerst1234</string>
XSLT1.0ソリューションは次のとおりです。
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output method="text"/>
<xsl:template match="string">
<xsl:call-template name="unique">
<xsl:with-param name="input" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template name="unique">
<xsl:param name="input"/>
<xsl:param name="output" select="''"/>
<xsl:variable name="c" select="substring($input, 1, 1)"/>
<xsl:choose>
<xsl:when test="not($input)">
<xsl:value-of select="$output"/>
</xsl:when>
<xsl:when test="contains($output, $c)">
<xsl:call-template name="unique">
<xsl:with-param name="input" select="substring($input, 2)"/>
<xsl:with-param name="output" select="$output"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="unique">
<xsl:with-param name="input" select="substring($input, 2)"/>
<xsl:with-param name="output" select="concat($output, $c)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
次の XPath ワンライナーを使用します。
codepoints-to-string(distinct-values(string-to-codepoints(.)))
これを使用した完全な変換は以下のとおりです。
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="text"/>
<xsl:template match="string">
<xsl:value-of select=
"codepoints-to-string(distinct-values(string-to-codepoints(.)))
"/>
</xsl:template>
</xsl:stylesheet>
この変換が最初に提供された XML ドキュメントに適用されると、次のようになります。
<root>
<string>aaeerstrst11232434</string>
</root>
必要な結果が生成されます:
aerst1234
XSLT 1.0 ソリューションが必要な場合は、その旨をお知らせください。提供します。
これは 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 の場合) の詳細についてはこちらを、FXSL 2.x (XSLT 2.0 の場合) についてはこちらをご覧ください。
より複雑なXMLを試してみると、 Martin Honnenのソリューションで多くの問題が発生しましたが、以下のXMLでは機能しないため、Dimitreの この回答を参照して独自のソリューションを準備 しました。より効率的なソリューション:
入力xmlは次のとおりです。
<root>
<string>aabcdbcd1abcdefghijklmanopqrstuvwxyzabcdefgh0123456789ijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz12312489796453134049446798421230156489413210315487804210313264046040489789789745648974321231564648971232344</string>
<string2>oejrinsjfojofjweofj24798273492jfakjflsdjljk</string2>
</root>
そして、これが動作中のXSLTコードです。
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:call-template name="unique_chars">
<xsl:with-param name="input" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template name="unique_chars">
<xsl:param name="input"/>
<xsl:variable name="c">
<xsl:value-of select="substring($input, 1, 1)"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="not($input)"/>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="contains(substring($input, 2), $c)">
<xsl:call-template name="unique_chars">
<xsl:with-param name="input" select="substring($input, 2)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$c"/>
<xsl:call-template name="unique_chars">
<xsl:with-param name="input" select="substring($input, 2)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>