xml


XSLT - Copy element which has maximum attribute value than other elements in list


I need to write XSl for the below input xml - Expected output should have the cluster element which has the maximum attribute (key)'s value.
Input:
<ProductList>
<Product ProductId="123">
<ClusterList>
<Cluster Key="1" Price="100.00"/>
<Cluster Key="3" Price="200.00"/>
<Cluster Key="2" Price="300.00"/>
</ClusterList>
</Product>
<Product ProductId="456">
<ClusterList>
<Cluster Key="11" Price="100.00"/>
<Cluster Key="33" Price="200.00"/>
<Cluster Key="22" Price="300.00"/>
</ClusterList>
</Product>
<ProductList>
Expected Output:
<ProductList>
<Product ProductId="123">
<ClusterList>
<Cluster Key="3" Price="200.00"/>
</ClusterList>
</Product>
<Product ProductId="456">
<ClusterList>
<Cluster Key="33" Price="200.00"/>
</ClusterList>
</Product>
<ProductList>
And here is the XSL which i have written, but not working :(
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="/ClusterList/Cluster">
<xsl:variable name="Max">
<xsl:value-of select="/ClusterList/Cluster[not(preceding-sibling::Cluster/#Key >= #Key) and not(following-sibling::Cluster/#Key > #Key)]/#Key" />
</xsl:variable>
<xsl:if test="#Key=$Max">
<xsl:copy>
<xsl:apply-templates select="#*" />
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
I would suggest you try it this way:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ClusterList">
<xsl:copy>
<xsl:for-each select="Cluster">
<xsl:sort select="#Key" data-type="number" order="descending"/>
<xsl:if test="position()=1">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
This is assuming you're using an XSLT 1.0 processor that does not support the EXSLT math:max() or math:highest() extension functions.
Note that in case of tie, only the first result will be returned.
To do it the way you have started, you would need to have:
<xsl:template match="Cluster">
<xsl:if test="not(preceding-sibling::Cluster/#Key > current()/#Key) and not(following-sibling::Cluster/#Key > current()/#Key)">
<xsl:copy>
<xsl:apply-templates select="#*" />
</xsl:copy>
</xsl:if>
</xsl:template>
which is very inefficient, as it has to compare every Cluster to all its siblings anew.

Related Links

X509Certificate2 validation on web service
Deleting XML elements in WiX
XML file with imbedded image data or ZIP file with XML + separate image files
MS Word splits words in its XML format
C# Xml Node to String
Best Practices of Uploading XML files in a Web-Application
atom feed xmlns attribute messes up AS3's XML-parsing?
Problem with StringBuilder and XML Literals
Xpath expression with multiple predicates
What is the practice for nesting xml namespaces?
Is there a “Fast Infoset” XML compression library for Delphi?
What is the worst abuse of XML that you have seen? [closed]
Syntax-highlighting for PXSL?
XML: attributes or children? [duplicate]
how to parse xml file with attributes?
Test to see if an xelement exists

Categories

HOME
microsoftgraph
cobalt
dns
nunit
google-spreadsheet
r-googlesheets
pdfbox
maxscript
pearson-correlation
sendgrid-api-v3
augeas
g++
categorical-data
activecollab
angular-dart
git-extensions
google-container-registry
jquery-ias
mongoid6
windows-server-2003
stackexchange
jackson-modules
fractions
iis-express
card.io
installshield-le
jwplayer6
imagenet
demo
trigonometry
plaintext
naming
chronicle-map
uiimage
fwrite
gitlist
identification
static-cast
bbpress
bootstrap-tags-input
choice
usart
grails-plugin
omxplayer
pacemaker
mongodb-php
redis-cluster
jbase
character-replacement
finalbuilder
webmatrix-3
contrast
webfinger
reflector
winginx
multiautocompletetextview
bin
eclipse-kepler
http-patch
cffile
neos-server
pclzip
wimax
dopostback
tree-balancing
boost-signals
oracle-enterprise-linux
failing-tests
jquery-address
procedures
entity-attribute-value
ikimagebrowserview
update-statement
quotation-marks
svk
squishit
cldc
fault-tolerance

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App