Reputation: 466
I know maybe there are too many questions regarding to that topic but i still couldn't find necessary XPath for my requirement...
I have an xml input like this :
<?xml version="1.0" encoding="utf-8"?>
<Products>
<product vib="SMS53L08TR">
<template name="PDF_ENLBL_GS_TR_NEWEULBL_FO">
<attr name="ProductSubFamily">Dishwashers</attr>
<attr name="ENERGY_CLASS_2010">EEK_EU_2010.A+</attr>
<attr name="ENERGY_CONS_ANNUAL_2010">290</attr>
<attr name="WATER_CONS_2010">3300</attr>
<attr name="PERF_DRY">TROCKNUNGSKLASSE.A</attr>
<attr name="SETTINGS">12</attr>
<attr name="NOISE">48</attr>
</template>
<template name="pdf.eudatasheet.gs.fo">
<attr name="ProductSubFamily">Dishwashers</attr>
<attr name="CODE">SMS53L08TR</attr>
<attr name="SETTINGS">12</attr>
<attr name="ENERGY_CLASS_2010">EEK_EU_2010.A+</attr>
<attr name="ENERGY_CONS_ANNUAL_2010">290</attr>
<attr name="ENERGY_CONS">1.02</attr>
<attr name="POWER_OFF_MODE_2010">0.10</attr>
<attr name="WATER_CONS_2010">3300</attr>
<attr name="PERF_DRY">TROCKNUNGSKLASSE.A</attr>
<attr name="REF_PRG">VERGLEICHSPROGRAMM.EC</attr>
<attr name="REF_PRG_TEMP">50</attr>
<attr name="CYCLE_TIME">195</attr>
<attr name="DURATION_LEFT_ON_MODE_2010">0</attr>
<attr name="NOISE">48</attr>
<attr name="CONSTR_TYPE">CONSTR_TYPE.Free-standing</attr>
</template>
</product>
<product vib="BM5201EG">
<template name="PDF_ENLBL_GS_TR_OLDLBL_FO">
<attr name="ProductSubFamily">Dishwashers</attr>
<attr name="ENERGY_CLASS">ENERGIEEFFIZIENZKLASSE.A</attr>
<attr name="PERF_CLEAN">REINIGUNGSKLASSE.A</attr>
<attr name="WATER_CONS">17.0</attr>
<attr name="ENERGY_CONS">1.05</attr>
<attr name="PERF_DRY">TROCKNUNGSKLASSE.A</attr>
<attr name="SETTINGS">12</attr>
<attr name="NOISE">57</attr>
</template>
<template name="pdf.eudatasheet.gs.fo">
<attr name="ProductSubFamily">Dishwashers</attr>
<attr name="CODE">BM5201EG</attr>
<attr name="SETTINGS">12</attr>
<attr name="ENERGY_CLASS_2010"/>
<attr name="ENERGY_CONS_ANNUAL_2010"/>
<attr name="ENERGY_CONS">1.05</attr>
<attr name="POWER_OFF_MODE_2010"/>
<attr name="WATER_CONS_2010"/>
<attr name="PERF_DRY">TROCKNUNGSKLASSE.A</attr>
<attr name="REF_PRG">VERGLEICHSPROGRAMM.EC</attr>
<attr name="REF_PRG_TEMP">50</attr>
<attr name="CYCLE_TIME">140</attr>
<attr name="DURATION_LEFT_ON_MODE_2010"/>
<attr name="NOISE">57</attr>
<attr name="CONSTR_TYPE">CONSTR_TYPE.Free-standing</attr>
</template>
</product>
</Products>
On this xml input i will try to get distinct values of templates :
<Template>PDF_ENLBL_GS_TR_NEWEULBL_FO</Template>
<Template>pdf.eudatasheet.gs.fo</Template>
<Template>PDF_ENLBL_GS_TR_OLDLBL_FO</Template>
What i am trying to do seems correct but still returns all values beside distinct(pdf.eudatasheet.gs.fo seems twice on output)... By the way $output is the variable name holds all xml document inside on it.
<xsl:for-each select="$output//product/template/@name[not(@name = preceding:: *)]">
What am i doing wrong on this XPath ? By the way as we need to stick on Xalan processor because of the environment that runs this xslt, i couldn't use distinct-values function of XSLT v2.0...
Upvotes: 1
Views: 308
Reputation: 116959
as we need to stick on Xalan processor because of the environment that runs this xslt, i couldn't use distinct-values function of XSLT v2.0.
If you are using the Xalan processor, you can take advantage of its own distinct()
extension function - for example:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="http://xml.apache.org/xalan"
extension-element-prefixes="xalan">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Products">
<root>
<xsl:for-each select="xalan:distinct(product/template/@name)">
<Template>
<xsl:value-of select="." />
</Template>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
Demo: http://xsltransform.net/bwdws6
You should most certainly not use a hack like:
<xsl:for-each select="$output//product/template/@name[not(@name = preceding:: *)]">
See why at http://www.jenitennison.com/xslt/grouping/muenchian.html where you can also learn a method that will work with any XSLT 1.0 processor.
Upvotes: 1
Reputation: 7385
You need to go one level higher, try this XPath:
select="$output//product/template[not(@name = preceding::template/@name)]/@name"
Upvotes: 1