Using XSLT to Concatenate Number Lists with Separators
Автор: vlogize
Загружено: 2025-09-28
Просмотров: 1
Описание:
Discover how to effectively use XSLT to transform a list of numbers in an XML attribute into a well-formatted string using commas and spaces.
---
This video is based on the question https://stackoverflow.com/q/63569657/ asked by the user 'Lter' ( https://stackoverflow.com/u/13517638/ ) and on the answer https://stackoverflow.com/a/63569853/ provided by the user 'Martin Honnen' ( https://stackoverflow.com/u/252228/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: XSLT : Concatenate a list of numbers with a separator according to a pattern
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Using XSLT to Concatenate Number Lists with Separators
If you're working with XML, you may have encountered a common problem: how to format a list of numbers properly. One such specific case involves transforming a Polygon element's @ POINTS attribute, which contains a series of numerical coordinates, into a more readable format.
The Problem
Consider the following XML snippet:
[[See Video to Reveal this Text or Code Snippet]]
In this example, the POINTS attribute consists of pairs of numbers representing coordinates, separated by spaces. What you want is to format this list into a string where each pair is separated by a comma, resulting in a zone element like this:
[[See Video to Reveal this Text or Code Snippet]]
The Solution
To solve this problem effectively using XSLT, you can leverage features available in both XSLT 2.0 and XSLT 3.0. Below, we’ll discuss both approaches.
Using XSLT 2.0
In XSLT 2.0, you can use the xsl:for-each-group function to tokenize the @ POINTS attributes and concatenate the numbers with a comma:
[[See Video to Reveal this Text or Code Snippet]]
Breakdown of the XSLT 2.0 Code:
xsl:template match="@ POINTS": This targets the @ POINTS attribute of the Polygon element.
tokenize(.): Divides the attribute's value into individual numbers based on spaces.
group-adjacent: Groups pairs of numbers together (e.g., the first number with the second).
string-join(current-group(), ','): Joins the grouped numbers with a comma.
Using XSLT 3.0
For those using XSLT 3.0, the solution is slightly streamlined with the for-each-pair function:
[[See Video to Reveal this Text or Code Snippet]]
Breakdown of the XSLT 3.0 Code:
let $tokens := tokenize(.): Similar to the 2.0 version, it breaks the numbers into a list.
for-each-pair: Iterates through every pair of coordinates.
function($a, $b) { $a || ',' || $b }: Concatenates each pair with a comma in between.
Conclusion
Transforming an XML attribute with a list of numbers into a formatted string can be efficiently achieved with XSLT. Whether you're using XSLT 2.0 or 3.0, these techniques will help you seamlessly format your data as desired. By applying the solutions outlined in this post, you can enhance the readability and usability of your XML data representations.
Now you're ready to implement this in your own XML transformations!
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: