I have collection of items:
<items>
<result>
<item>
<product_name>some description</product_name>
</item>
<item>
<product_name>some similar description</product_name>
</item>
<item>
<product_name>other product size 1</product_name>
</item>
<item>
<product_name>other product size 2</product_name>
</item>
</result>
</items>
I also have some external function strdist:string-distance that compares previous product to current one and if there is some match then it returns true. Based on this returned value I'd like to:
add the current element of for-each to the group of previous element when returned value is true
close previous group, create a new group and add current element of for-each loop to it when returned value is false
I'm little bit struggling with the process of how to create the groups and add there elements in for-loop.
Here is my template
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:strdist="http://example.com/string-distance"
exclude-result-prefixes="strdist">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:variable name="items" as="element()*">
<xsl:perform-sort select="items/result/item">
<xsl:sort select="./product_name"/>
</xsl:perform-sort>
</xsl:variable>
<xsl:variable name="groupedItems" as="element()*">
<groups>
<xsl:for-each select="$items">
<xsl:variable name="position" select="position()"/>
<xsl:variable name="currentProductName" select="./product_name/text()"/>
<xsl:choose>
<xsl:when test="$position -1 = 0">
<xsl:element name="group"/>
<xsl:message select="concat($position, ' # ', $currentProductName)"/>
</xsl:when>
<xsl:when test="$position -1 > 0">
<xsl:variable name="previousProductName"
select="$items[position() = $position -1]/product_name/text()"/>
<xsl:message
select="concat($position, ' # ', $previousProductName, ' # ', $currentProductName)"/>
<xsl:message select="strdist:string-distance($previousProductName, $currentProductName)"/>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</groups>
</xsl:variable>
<xsl:message select="$groupedItems" />
</xsl:template>
</xsl:stylesheet>
Finally I'd like to have something like this:
<items>
<result>
<group>
<item>
<product_name>some description</product_name>
</item>
<item>
<product_name>some similar description</product_name>
</item>
</group>
<group>
<item>
<product_name>other product size 1</product_name>
</item>
<item>
<product_name>other product size 2</product_name>
</item>
</group>
</result>
</items>
I'm using xslt 2.0 with saxon-he 10.3.
Here is the example using for-each-group group-starting-with and a sample function:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="#all"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output method="xml" indent="yes"/>
<xsl:function name="mf:string-distance" as="xs:boolean">
<xsl:param name="name1" as="xs:string"/>
<xsl:param name="name2" as="xs:string"/>
<xsl:sequence select="tokenize($name1)[1] = tokenize($name2)[1]"/>
</xsl:function>
<xsl:template match="result">
<xsl:copy>
<xsl:for-each-group select="item" group-starting-with="item[not(mf:string-distance(product_name, preceding-sibling::item[1]/product_name))]">
<group>
<xsl:apply-templates select="current-group()"/>
</group>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Seems to give the wanted result at https://xsltfiddle.liberty-development.net/bEJbVrR.
Related
I have XML in which there is a ContactRecords node:
<Organisations>
<Organisation>
<Tag1>ValueElementTag1</Tag1>
<Tag2>ValueElementTag2</Tag2>
<Tag3>ValueElementTag3</Tag3>
<ContactRecords>
<item>
<ContactRecordType>AAAAA</ContactRecordType>
<ContactValue>ValueAAAAA</ContactValue>
<Address xmlns="http://www.v8.1c.ru/ssl/contactinfo" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<AdrTag1 xsi:type="Adr">Example1</AdrTag1>
<AdrTag2>Example2</AdrTag2>
</Address>
</item>
<item>
<ContactRecordType>BBBBB</ContactRecordType>
<ContactValue>ValueBBBBB</ContactValue>
<Address xmlns="http://www.v8.1c.ru/ssl/contactinfo" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<AdrTag1 xsi:type="Adr">Example1</AdrTag1>
<AdrTag2>Example2</AdrTag2>
</Address>
</item>
<item>
<ContactRecordType>CCCCC</ContactRecordType>
<ContactValue>ValueCCCCC</ContactValue>
</item>
</ContactRecords>
</Organisation>
<Organisation>
<Tag1>ValueElementTag1</Tag1>
<Tag2>ValueElementTag2</Tag2>
<Tag3>ValueElementTag3</Tag3>
<ContactRecords>
<item>
<ContactRecordType>AAAAA</ContactRecordType>
<ContactValue>ValueAAAAA</ContactValue>
<Address xmlns="http://www.v8.1c.ru/ssl/contactinfo" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<AdrTag1 xsi:type="Adr">Example1</AdrTag1>
<AdrTag2>Example2</AdrTag2>
</Address>
</item>
<item>
<ContactRecordType>BBBBB</ContactRecordType>
<ContactValue>ValueBBBBB</ContactValue>
<Address xmlns="http://www.v8.1c.ru/ssl/contactinfo" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<AdrTag1 xsi:type="Adr">Example1</AdrTag1>
<AdrTag2>Example2</AdrTag2>
</Address>
</item>
<item>
<ContactRecordType>CCCCC</ContactRecordType>
<ContactValue>ValueCCCCC</ContactValue>
</item>
</ContactRecords>
</Organisation>
</Organisations>
I am writing an XSLT which has a handling of a ContactRecords node:
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:element name="Organisations">
<xsl:for-each select="Organisations/Organisation">
<xsl:element name="{name(.)}">
<xsl:for-each select="*[not(name()='ContactRecords')]">
<xsl:copy select="*">
<xsl:value-of select="normalize-space(.)"/>
</xsl:copy>
</xsl:for-each>
<xsl:for-each select="ContactRecords/item">
<xsl:choose>
<xsl:when test="Address">
<h2>mooooooooooooo</h2>
</xsl:when>
<xsl:otherwise>
<h2>dooooooooooooo</h2>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:transform>
I am now getting the following result:
<h2>dooooooooooooo</h2>
<h2>dooooooooooooo</h2>
<h2>dooooooooooooo</h2>
I expect to receive:
<h2>mooooooooooooo</h2>
<h2>mooooooooooooo</h2>
<h2>dooooooooooooo</h2>
What am I doing wrong?
If I explain the algorithm in words, then I need the following: if there is an Address element in the item element, then we do logic number 1. If there is no Address element in the item element, then we do logic number 2.
If we describe the algorithm in pseudocode, then this is how:
if (item.includes(Address)) {
do logic #1
} else {
do logic #2
}
UPD1: Updated XML and XSLT code
UPD2: Add namespaces in tags Address (maybe the reason is in them)
The Address element is in a namespace, so your test:
<xsl:when test="Address">
returns false every time. Try it this way (minimized to the current problem):
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://www.v8.1c.ru/ssl/contactinfo"
exclude-result-prefixes="ns0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Organisations">
<Organisations>
<xsl:for-each select="Organisation">
<xsl:copy>
<!-- omitted -->
<xsl:for-each select="ContactRecords/item">
<h2>
<xsl:choose>
<xsl:when test="ns0:Address">mooooooooooooo</xsl:when>
<xsl:otherwise>dooooooooooooo</xsl:otherwise>
</xsl:choose>
</h2>
</xsl:for-each>
</xsl:copy>
</xsl:for-each>
</Organisations>
</xsl:template>
</xsl:stylesheet>
I'm trying to get my head over nested grouping and sorting while using for-each-group.
My idea is to order and group items at first by producer. Then when I have this producer groups I'd like to sort each of them by code. However currently the order of code doesn't work as I'd like to. In following example the problem is with item with code=01001-064-03. It should be grouped together with all other items whose code starts with 01001 but it isn't. If I move entire item/code[text()='01001-064-03'] (the last one) to the beginning of xml then grouping works ok.
Please what is my issue here?
Thanks
<items>
<change_date>#11.11.2020 7:42:13</change_date>
<result>
<item>
<code>01001-064-01</code>
<producer>prod1</producer>
</item>
<item>
<code>01001-064-02</code>
<producer>prod1</producer>
</item>
<item>
<code>def</code>
<producer>prod1</producer>
</item>
<item>
<code>ghi</code>
<producer>prod2</producer>
</item>
<item>
<code>jkl</code>
<producer>prod3</producer>
</item>
<item>
<code>abc</code>
<producer>prod3</producer>
</item>
<item>
<code>def</code>
<producer>prod4</producer>
</item>
<item>
<code>ghi</code>
<producer>prod4</producer>
</item>
<item>
<code>jkl</code>
<producer>prod5</producer>
</item>
<item>
<code>01001-064-03</code>
<producer>prod1</producer>
</item>
</result>
</items>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:array="http://www.w3.org/2005/xpath-functions/array"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="#all"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output method="xml" indent="yes" html-version="5"/>
<xsl:function name="mf:same-product" as="xs:boolean">
<xsl:param name="left" as="xs:string"/>
<xsl:param name="right" as="xs:string"/>
<xsl:variable name="leftParsed" select="mf:get-regexp-group($left, 1)"/>
<xsl:variable name="rightParsed" select="mf:get-regexp-group($right, 1)"/>
<xsl:sequence select="matches($leftParsed, $rightParsed)"/>
</xsl:function>
<xsl:function name="mf:get-regexp-group" as="xs:string">
<xsl:param name="text" as="xs:string"/>
<xsl:param name="groupNumber" as="xs:integer"/>
<xsl:variable name="result">
<xsl:analyze-string select="$text" regex="(^[a-zA-Z0-9]+)(.*)">
<xsl:matching-substring>
<xsl:value-of select="regex-group($groupNumber)"/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:variable>
<xsl:sequence select="$result"/>
</xsl:function>
<xsl:template match="items">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="change_date"/>
<xsl:template match="result">
<data>
<xsl:for-each-group select="item" group-by="producer">
<xsl:sort select="producer"/>
<xsl:for-each-group select="current-group()" group-starting-with="item[not(mf:same-product(code, preceding-sibling::item[1]/code))]">
<xsl:sort select="code"/>
<group>
<xsl:apply-templates select="current-group()" />
</group>
</xsl:for-each-group>
</xsl:for-each-group>
</data>
</xsl:template>
<xsl:template match="item">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
fiddle example here
I'm using xslt 2.0 with saxon-he 10.3.
EDIT:
So as #michael.hor257k asked for better explanation I'll try to do my best:
Each item is product. This product has producer and has code (product code). I want to group all products of producer by code. However codes are not same for similar products so the similarity is matched by function mf:same-product. For example two similar products could be 01001-064-01 and 01001-064-02 here I check the first prefix 01001 and if it matches it means both products should be added to same group.
expected result should look like this:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<group>
<item>
<code>01001-064-01</code>
<producer>prod1</producer>
</item>
<item>
<code>01001-064-02</code>
<producer>prod1</producer>
</item>
<item>
<code>01001-064-03</code>
<producer>prod1</producer>
</item>
</group>
<group>
<item>
<code>def</code>
<producer>prod1</producer>
</item>
</group>
<group>
<item>
<code>ghi</code>
<producer>prod2</producer>
</item>
</group>
<group>
<item>
<code>abc</code>
<producer>prod3</producer>
</item>
</group>
<group>
<item>
<code>jkl</code>
<producer>prod3</producer>
</item>
</group>
<group>
<item>
<code>def</code>
<producer>prod4</producer>
</item>
</group>
<group>
<item>
<code>ghi</code>
<producer>prod4</producer>
</item>
</group>
<group>
<item>
<code>jkl</code>
<producer>prod5</producer>
</item>
</group>
</data>
Perhaps a composite group-by suffices:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:mode on-no-match="shallow-skip"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="change_date"/>
<xsl:template match="result">
<data>
<xsl:for-each-group select="item" composite="yes" group-by="producer, code => replace('[^a-z0-9].*$', '', 'i')">
<xsl:sort select="producer"/>
<xsl:sort select="code"/>
<group>
<xsl:apply-templates select="current-group()" />
</group>
</xsl:for-each-group>
</data>
</xsl:template>
<xsl:template match="item">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/ei5R4uT/10
That is XSLT 3 which Saxon 9.8 and later (e.g. Saxon 10) support, if you really need to do it with an XSLT 2.0 processor then a nested for-each-group group-by or a concatenated grouping key can achieve the same as the composite grouping key in the above XSLT 3.
My idea is to order and group items at first by producer. Then when I have this producer groups I'd like to sort each of them by code.
If that's all you want to do, why isn't it sufficient to do:
XSLT 3.0
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="result">
<xsl:for-each-group select="item" group-by="producer">
<xsl:sort select="producer"/>
<group>
<xsl:apply-templates select="current-group()">
<xsl:sort select="code"/>
</xsl:apply-templates>
</group>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
I'm using xslt 2.0 with saxon-he 10.3.
Actually, you are using XSLT 3.0.
I have a group of dates and I'd like to create partitions with a criterion such as "exactly 7 days apart" For example this is my source xml:
<root>
<entry date="2019-05-12" />
<entry date="2019-05-19" />
<entry date="2019-05-26" />
<entry date="2019-06-16" />
<entry date="2019-06-23" />
</root>
The result should be like this:
<root>
<group>
<val>12.5.</val>
<val>19.5.</val>
<val>26.5.</val>
</group>
<group>
<val>16.6.</val>
<val>23.6.</val>
</group>
</root>
since the first three and the last two dates are all on a Sunday without a gap.
What I have so far is this:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:sd="urn:someprefix"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
>
<xsl:output indent="yes"/>
<xsl:template match="root">
<root>
<xsl:copy-of select="sd:partition(distinct-values(for $i in entry/#date return $i cast as xs:date))"/>
</root>
</xsl:template>
<xsl:function name="sd:partition">
<xsl:param name="dates" as="xs:date*"/>
<xsl:for-each-group select="$dates" group-adjacent="format-date(., '[F]')">
<group>
<xsl:for-each select="current-group()">
<val>
<xsl:value-of select="format-date(.,'[D].[M].')"/>
</val>
</xsl:for-each>
</group>
</xsl:for-each-group>
</xsl:function>
</xsl:stylesheet>
Which only generates one group.
How can I ask for the previous element to be 7 days apart? I know of duration (xs:dayTimeDuration('P1D')), but I don't know how to compare it to a previous value.
I use Saxon 9.8 HE.
I think you can also do it using group-adjacent:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
expand-text="yes"
version="3.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="root">
<xsl:copy>
<xsl:for-each-group select="entry/#date/xs:date(.)"
group-adjacent=". - (position() - 1) * xs:dayTimeDuration('P7D')">
<group>
<xsl:apply-templates select="current-group()"/>
</group>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match=".[. instance of xs:date]">
<val>{format-date(.,'[D].[M].')}</val>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/ncdD7mM
To do your grouping, you really need to know the difference in days with the previous element, then you can group starting with dates where the difference is not 7 days. So, you can declare a variable where you build up some new XML with the dates and differences, and then use that to group.
Try this function in your XSLT instead.
<xsl:function name="sd:partition">
<xsl:param name="dates" as="xs:date*"/>
<xsl:variable name="datesWithDiff" as="element()*">
<xsl:for-each select="$dates">
<xsl:variable name="pos" select="position()" />
<date diff="{(. - $dates[$pos - 1]) div xs:dayTimeDuration('P1D')}">
<xsl:value-of select="." />
</date>
</xsl:for-each>
</xsl:variable>
<xsl:for-each-group select="$datesWithDiff" group-starting-with="date[#diff = '' or xs:int(#diff) gt 7]">
<group>
<xsl:for-each select="current-group()">
<val>
<xsl:value-of select="format-date(.,'[D].[M].')"/>
</val>
</xsl:for-each>
</group>
</xsl:for-each-group>
</xsl:function>
I have the following xml sample:
<items>
<item>
<item_id>1</item_id>
<item_name>item 1</item_name>
<group_id>1</group_id>
<group_name>group 1</group_name>
</item>
<item>
<item_id>2</item_id>
<item_name>item 2</item_name>
<group_id>1</group_id>
<group_name>group 1</group_name>
</item>
<item>
<item_id>3</item_id>
<item_name>item 3</item_name>
<group_id>2</group_id>
<group_name>group 2</group_name>
</item>
</items>
which I need transformed into the following csv format:
1,item 1
2,item 2
3,item 3
1,group 1
2,group 2
In the xml, item_id will always be followed by item_name. The item name will not always be concat('item_',#). It could be an description such as 'toothpaste'. Items will never be repeated in the file but occasionally the group_id->group_name pairing will not always be 1-1. In this case, it is preferred to take the first pairing.
I am accomplishing this using a 'for-each-group' statement but it seems a bit hacky. Are there any downsides to this approach? What are some better ways to accomplish this?
<xsl:template match="/">
<xsl:call-template name="list_format">
<xsl:with-param name="list" select="'item'"/>
</xsl:call-template>
<xsl:call-template name="list_format">
<xsl:with-param name="list" select="'group'"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="list_format">
<xsl:param name="list"/>
<xsl:variable name="linefeed" select="'
'"></xsl:variable>
<xsl:variable name="list_id" select="concat($list,'_id')"></xsl:variable>
<xsl:variable name="list_name" select="concat($list,'_name')"></xsl:variable>
<xsl:for-each-group select="/items/item" group-by="*[name()=$list_id]">
<xsl:sort select="*[name()=$list_id]"/>
<xsl:value-of select="*[name()=$list_id]"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="*[name()=$list_name]"/>
<xsl:value-of select="$linefeed"/>
</xsl:for-each-group>
</xsl:template>
hmmm .. not really a downside but it doesn't work in XSLT 1.0 as it doesn't support for-each-group :-/
I don't think you would need XSLT 1.0 solution .. but still am posting my work :) Check this out!
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/items">
<xsl:call-template name="items_grouping">
<xsl:with-param name="list" select="'item'"/>
</xsl:call-template>
<xsl:call-template name="items_grouping">
<xsl:with-param name="list" select="'group'"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="items_grouping">
<xsl:param name="list"/>
<xsl:variable name="linefeed" select="'
'"></xsl:variable>
<xsl:variable name="list_id" select="concat($list,'_id')"></xsl:variable>
<xsl:variable name="list_name" select="concat($list,'_name')"></xsl:variable>
<xsl:for-each select="item">
<xsl:for-each select="*[name()=$list_id and not(.= ../preceding-sibling::item/*[name()=$list_id]/.)]">
<xsl:value-of select="concat(.,',')"/>
<xsl:value-of select="../*[name()=$list_name]"/>
<xsl:value-of select="$linefeed"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
Used same methodology of yours .. only change is 'preceding-sibling'
and also for this code, order of child-nodes of <item> doesn't matter
Here is a simple and short XSLT 2.0 (as requested) solution:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates/>
<xsl:for-each-group select="/*/*/group_id" group-by=".">
<xsl:value-of separator="," select="../*[starts-with(name(), 'group_')]"/>
<xsl:text>
</xsl:text>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="item">
<xsl:value-of separator="," select="*[starts-with(name(), 'item_')]"/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the provided XML document:
<items>
<item>
<item_id>1</item_id>
<item_name>item 1</item_name>
<group_id>1</group_id>
<group_name>group 1</group_name>
</item>
<item>
<item_id>2</item_id>
<item_name>item 2</item_name>
<group_id>1</group_id>
<group_name>group 1</group_name>
</item>
<item>
<item_id>3</item_id>
<item_name>item 3</item_name>
<group_id>2</group_id>
<group_name>group 2</group_name>
</item>
</items>
the wanted, correct result is produced:
1,item 1
2,item 2
3,item 3
1,group 1
2,group 2
I have an xml file like this:
<root>
<item>
<name>one</name>
<status>good</status>
</item>
<item>
<name>two</name>
<status>good</status>
</item>
<item>
<name>three</name>
<status>bad</status>
</item>
<item>
<name>four</name>
<status>ugly</status>
</item>
<item>
<name>five</name>
<status>bad</status>
</item>
</root>
I want to transform this using XSLT to get something like:
<root>
<items><status>good</status>
<name>one</name>
<name>two</name>
</items>
<items><status>bad</status>
<name>three</name>
<name>five</name>
</items>
<items><status>ugly</status>
<name>four</name>
</items>
</root>
In other words, I get a list of items, each with a status, and I want to turn it into a list of statuses, each with a list of items.
My initial thought was to do apply-templates matching each status type in turn, but that means I have to know the complete list of statuses. Is there a better way to do it?
Thanks for any help.
Muench to the rescue!
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:key name="muench" match="/root/item/status" use="."/>
<xsl:template match="/">
<root>
<xsl:for-each select="/root/item/status[generate-id() = generate-id(key('muench',.)[1])]">
<xsl:call-template name="pivot">
<xsl:with-param name="status" select="."/>
</xsl:call-template>
</xsl:for-each>
</root>
</xsl:template>
<xsl:template name="pivot">
<xsl:param name="status"/>
<items>
<status><xsl:value-of select="$status"/></status>
<xsl:for-each select="/root/item[status=$status]">
<name><xsl:value-of select="name"/></name>
</xsl:for-each>
</items>
</xsl:template>
</xsl:stylesheet>
Yes, this can be done in XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<!-- -->
<xsl:key name="kStatByVal"
match="status" use="."/>
<!-- -->
<xsl:key name="kItemByStat"
match="item" use="status"/>
<!-- -->
<xsl:variable name="vDoc" select="/"/>
<xsl:template match="/">
<top>
<xsl:for-each select=
"/*/*/status[generate-id()
=
generate-id(key('kStatByVal',.)[1])
]">
<items>
<status><xsl:value-of select="."/></status>
<xsl:for-each select="key('kItemByStat', .)">
<xsl:copy-of select="name"/>
</xsl:for-each>
</items>
</xsl:for-each>
</top>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the original XML document:
<root>
<item>
<name>one</name>
<status>good</status>
</item>
<item>
<name>two</name>
<status>good</status>
</item>
<item>
<name>three</name>
<status>bad</status>
</item>
<item>
<name>four</name>
<status>ugly</status>
</item>
<item>
<name>five</name>
<status>bad</status>
</item>
</root>
The wanted result is produced:
<top>
<items>
<status>good</status>
<name>one</name>
<name>two</name>
</items>
<items>
<status>bad</status>
<name>three</name>
<name>five</name>
</items>
<items>
<status>ugly</status>
<name>four</name>
</items>
</top>
Do note the use of:
The Muenchian method for grouping
The use of <xsl:key> and the key() function
It depends about your xslt engine. If you're using xslt 1.0 without any extension, then your approach is certainly the best.
On the other side, if you're allowed to use exslt (especially the node-set extension) or xslt 2.0, then you could do it in a more generic way:
Collect all the available statuses
Create a node-set from the obtained result
Iterating on this node set, create your pivot by filtering you status base on the current element in your iteration.
But before doing that, consider that it may be overkill if you only have a few set of statuses and that adding another status is quite rare.
In XSLT 2.0 you can replace the muenchian grouping by its standard grouping mechanism. Applied to the given answer the xslt would look like:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:key name="muench" match="/root/item/status" use="."/>
<xsl:template match="/">
<root>
<xsl:for-each-group select="/root/item/status" group-by="key('muench', .)">
<xsl:call-template name="pivot">
<xsl:with-param name="status" select="."/>
</xsl:call-template>
</xsl:for-each-group>
</root>
</xsl:template>
<xsl:template name="pivot">
<xsl:param name="status"/>
<items>
<status><xsl:value-of select="$status"/></status>
<xsl:for-each select="/root/item[status=$status]">
<name><xsl:value-of select="name"/></name>
</xsl:for-each>
</items>
</xsl:template>
</xsl:stylesheet>