

Separate values of the sequence using the XSLT function
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.

Separate values of the sequence using the XSLT function
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
-
2
利用XSLT继续击垮XML 小飞 ·...
-
37
Recently I was tasked with doing a web app test for a large organization. The application itself was not of great interest given that it only had a few dynamic parameters but instead the application stack was where my int...
-
9
Study Notes Weekly No.4(Use tracker to load dll & Use csi to bypass UMCI & Execute C# from XSLT file) About: use tracker to load dll use csi to bypa...
-
7
XSLT eliminates parents' children dynamically advertisements To avoid having to hard code the child element names I want to remove, I'd like t...
-
8
Using xslt to interpret the flat xml in the nested xhtml advertisements I am attempting to write XSLT that will select immediate-siblings of a...
-
5
Find All Separate Values in User-Based Selection - Excel VBA advertisements is there a quick and easy way to select all distinct values with...
-
3
The SEQUENCE function seems deceptively simple but is a powerful new function with some surprisingly flexible applications for dynamic arrays. This function generates a sequence of values using the specified optional start an...
-
4
Reset a PostgreSQL sequence and update column values Yesterday, I understood that I had broken a sequence of an auto-increment column in my PostgreSQL database. This quick tip will show you how to reset the sequence, restart...
-
8
Short Note There are good reasons and a relatively long history, if you want to scoop up sporadic early attempts. It's called rationalization and its activity really picked up after WW II and the new-found need for manufacturing...
-
9
@maratMaratHi guys, I'm Marat, a software developer with expertise i...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK