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

Combining several XPaths into a single XPath
How to return data XML schema defination in WCF?
Shell | removing repetitive lines
Is it wrong in XML to have element nodes as siblings of text nodes?
XPath predicate using last function
handling html nested list element using perl XML::Twig
F# Xml Type Provider test if node exists
how to send a XML message with PHP mailer
R can this closure code be made more efficient?
If then else in XPath 1.0
Parsing an XML file without using modules
How to copy all but with exceptions
Grouping some XML elements with XSLT
XML UTF-8 encoding lost after Xpath
XQuery join result
How can I recursively nest XML nodes, based on an attribute value, using XSLT?

Categories

HOME
azure-stream-analytics
scikit-learn
c++11
ng-idle
character-encoding
fftw
appcelerator
cakephp-3.4
limit
ado.net
settings
text-mining
rtf
radgridview
renjin
entitymanager
xmonad
production
replication
asciidoctor-pdf
googlebot
parcelable
visual-studio-extensions
comparator
dynamically-generated
siri
opentk
gpio
oculus
firebase-analytics
inversion-of-control
browser-sync
winpe
finagle
ntp
autodesk-designautomation
panoramas
mobx
searchkit
xcode7.3
filesystemwatcher
gdata
tasm
plaintext
berkeley-db-je
signal-strength
oim
procfile
phpdbg
servermanager
netbeans6.8
identification
convex-optimization
posixct
qtranslate
mach-o
shinobi
filenet
reachability
cudd
hadoop-2.7.2
preprocessor
segment-io
watch-os-2
gogs
nservicebus5
angular-ui-typeahead
httpruntime.cache
design-principles
computer-architecture
oracle-spatial
android-launcher
cgi-bin
rpart
keyup
botan
dnssec
omnifaces
google-refine
user-permissions
phpldapadmin
code-documentation
ssis-data-flow
genymotion-call
imagefilter
tweenlite
create.js
authlogic
extjs2
koala
process-explorer
jquery-1.9
mysql-error-2013
bjyauthorize
resource-management
fragmenttransaction
saleslogix
paginator
boost-signals
stress
multiplatform
uipagecontrol
cac
undefined-index
object-database
dot-emacs
aspmenu
pos-for-.net
scrubyt

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