4

Separate values ​​of the sequence using the XSLT function

 3 years ago
source link: https://www.codesd.com/item/separate-values-of-the-sequence-using-the-xslt-function.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Separate values ​​of the sequence using the XSLT function

advertisements

I'm trying to get the unique values of a custom generated sequence of strings in XSLT. The problem at hand is rather unusual since I have to first split a string on '.' and chop of the last bit, that's working fine but getting the unique values using the XSLT 2.0 distinct-values is not.

Given some input

<TreeNumberList>
  <TreeNumber>A01.001.001</TreeNumber>
  <TreeNumber>A01.001.002</TreeNumber>
  <TreeNumber>A01.001.003</TreeNumber>
  <TreeNumber>A01.002.111</TreeNumber>
</TreeNumberList>

The desired output would be an iterable sequence of

A01.001, A01.002

So far I have the following function

<xsl:function name="func:strip-last">
    <xsl:param name="str"></xsl:param>
    <xsl:value-of select="substring($str, 1, string-length($str) - 1)"></xsl:value-of>
</xsl:function>

<xsl:function name="func:parent-of">
    <xsl:param name="nodes"></xsl:param>
    <xsl:variable name="output">
    <xsl:for-each select="$nodes">
        <xsl:variable name="parent">
            <xsl:for-each select="tokenize(., '\.')">
                <xsl:if test="position() != last()">
                    <xsl:value-of select="."></xsl:value-of>
                    <xsl:text>.</xsl:text>
                </xsl:if>
            </xsl:for-each>
        </xsl:variable>
        <tmp><xsl:value-of select="func:strip-last($parent))"></xsl:value-of></tmp>
    </xsl:for-each>
    </xsl:variable>
    <xsl:sequence select="distinct-values($output/*)"></xsl:sequence>
</xsl:function>

But this does not return a set of distinct elements, rather a sequence of all the elements involved. The eventual code will be a bit more contrived since the TreeNumbers themselves will not be unique but a some name retrieved through a key lookup will be. (For those recognizing the mark-up, it's part of the MeSH XML)

I've also tried to use a key-ed index or group-by for uniqueness but those did not play well with the document fragments.


The following expression gives the set of distinct values as a sequence of strings:

distinct-values(TreeNumber/replace(., '\..*$', ''))


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK