xml - XSLT 2.0 - Distinct values using a key -
i'm having bit of troubles working on xslt sheet. xml document :
<?xml version="1.0" encoding="utf-8"?> <catalog> <products> <product> <id>1</id> </product> </products> <stocks> <stock> <id>1</id> <size>s</size> <store>nyc</store> </stock> <stock> <id>1</id> <size>l</size> <store>nyc</store> </stock> <stock> <id>1</id> <size>s</size> <store>la</store> </stock> </stocks> </catalog> what want have kind of output xml :
<?xml version="1.0" encoding="utf-8"?> <catalog> <products> <product> <id>1</id> <variants> <variant> <size>s</size> <stocks> <stock store-ref="nyc"> <stock store-ref="la"> </stocks> <variant> <variant> <size>l</size> <stocks> <stock store-ref="nyc"> </stocks> <variant> </variants> </product> </products> </catalog> today, i'm using xslt perform transformation :
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="2.0" exclude-result-prefixes="xs fn" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:fn="http://www.w3.org/2005/xpath-functions"> <xsl:key name="sizes" match="stock" use="id"/> <xsl:key name="stocks" match="stock" use="fn:concat(id, '-', size)"/> <xsl:output method="xml" encoding="utf-8" indent="yes"/> <xsl:template match="/"> <catalog> <products> <xsl:for-each select="/catalog/products/product"> <product> <id><xsl:value-of select="id" /></id> <variants> <xsl:for-each select="key('sizes', id)"> <variant> <size><xsl:value-of select="size" /></size> <stocks> <xsl:for-each select="key('stocks', fn:concat(id, '-', size))"> <stock store-ref="{store}" /> </xsl:for-each> </stocks> </variant> </xsl:for-each> </variants> </product> </xsl:for-each> </products> </catalog> </xsl:template> </xsl:stylesheet> and got result :
<?xml version="1.0" encoding="utf-8"?> <catalog> <products> <product> <id>1</id> <variants> <variant> <size>s</size> <stocks> <stock store-ref="nyc"/> <stock store-ref="la"/> </stocks> </variant> <variant> <size>l</size> <stocks> <stock store-ref="nyc"/> </stocks> </variant> <variant> <size>s</size> <stocks> <stock store-ref="nyc"/> <stock store-ref="la"/> </stocks> </variant> </variants> </product> </products> </catalog> so problem i'd select distinct size values not seem work. i've tried utilize generate-id() don't understand how works didn't had great results :( thought how prepare ? !
thanks @tomalak, i've found solution. here working xslt :
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="2.0" exclude-result-prefixes="xs fn" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:fn="http://www.w3.org/2005/xpath-functions"> <xsl:key name="sizes" match="stock" use="id"/> <xsl:key name="stocks" match="stock" use="fn:concat(id, '-', size)"/> <xsl:output method="xml" encoding="utf-8" indent="yes"/> <xsl:template match="/"> <catalog> <products> <xsl:for-each select="/catalog/products/product"> <product> <id><xsl:value-of select="id" /></id> <variants> <xsl:for-each-group select="key('sizes', id)" group-by="size"> <variant> <size><xsl:value-of select="size" /></size> <stocks> <xsl:for-each select="key('stocks', fn:concat(id, '-', size))"> <stock store-ref="{store}" /> </xsl:for-each> </stocks> </variant> </xsl:for-each-group> </variants> </product> </xsl:for-each> </products> </catalog> </xsl:template> </xsl:stylesheet> xml xslt xslt-2.0 xslkey
No comments:
Post a Comment