XSLT How to get previous attribute value if my condition satisfied?
I have tried to get the desired out but not succeeded can someone please assist me how to achieve this?
Here is my Zip.xml
<Zip>
<ZipNotify Zip="1144" ZipNo="1" ZipTime="2010-09-02T11:15:30+10:00"/>
<ZipDetail Zone="U" DepartZip="West" ArriveZip="West"/>
<ZipCategoryDetail ZORE="false" />
<ZipOrigin ZipOrigin="ABC002" Die="20:59:00"/>
<ZipDestination ZipDestination="UVW001" Live="21:38:00"/>
<ZipPath>
<ZipSubject ZipSubjectType="Payed" ZipNum="1">
<ZipRoute ZipLoc="ABC002" ZipStop="true" ZipDieTime="20:59:00" ZipDieTime1="20:59:00"/>
</ZipSubject>
<ZipSubject ZipSubjectType="Payed" ZipNum="2">
<ZipRoute ZipLoc="BCD002" ZipStop="true" ZipLiveTime="21:00:40" ZipDieTime1="21:01:00"/>
<ZipSpec Charge="false"/>
</ZipSubject>
<ZipSubject ZipSubjectType="Payed" ZipNum="3">
<ZipRoute ZipLoc="CDE001" ZipStop="true" ZipLiveTime="21:03:40" ZipDieTime1="21:04:00"/>
<ZipSpec Charge="true"/>
</ZipSubject>
<ZipSubject ZipSubjectType="Payed" ZipNum="4">
<ZipRoute ZipLoc="DEF001" ZipStop="true" ZipLiveTime="21:05:40" ZipDieTime1="21:06:00"/>
<ZipSpec Charge="true"/>
</ZipSubject>
<ZipSubject ZipSubjectType="Payed" ZipNum="5">
<ZipRoute ZipLoc="EFG001" ZipStop="true" ZipLiveTime="21:07:40" ZipDieTime1="21:08:00"/>
<ZipSpec Charge="true"/>
</ZipSubject>
<ZipSubject ZipSubjectType="Payed" ZipNum="5">
<ZipRoute ZipLoc="UVW001" ZipStop="true" ZipLiveTime="21:38:00" ZipLiveTime1="21:38:00"/>
<ZipSpec Charge="true"/>
</ZipSubject>
</ZipPath>
</Zip>
Here is my XSL
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head/>
<body>
<xsl:for-each select="/Zip/ZipPath/ZipSubject">
<xsl:if test="/Zip/ZipPath/ZipSubject/ZipSpec/@Charge = 'true'">
<div>ZipLoc: <xsl:value-of select="/Zip/ZipPath/ZipSubject/ZipRoute/@ZipLoc"/> </div>
<div>Charge: <xsl:value-of select="/Zip/ZipPath/ZipSubject/ZipSpec/@Charge"/></div>
</xsl:if>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Out Put I want is
I want to get the ZipLoc of previous ZipSubject where Charge=”true”
Eg. For ZipNum=”3″ value Charge=”true” so my out put must display previous ZipLoc=”BCD002″ (even though Charge=”false” ZipNum=”2″) that’s it, it should not do further validation for ZipOrigin and for ZipDestination again it should check from the last ZipNum=”5″ if Charge=”true” then simply it should use the same ZipLoc=”UVW001″
Desired Output should be
BCD002UVW001
But my output is below. I have tried possible ways(I know its lack of experience with XSLT which is not an excuse) please help me to get the Desired Output
ZipLoc: ABC002
Charge: false
ZipLoc: ABC002
Charge: false
ZipLoc: ABC002
Charge: false
ZipLoc: ABC002
Charge: false
ZipLoc: ABC002
Charge: false
ZipLoc: ABC002
Charge: false
Inside the context of
xsl:for-eachyou can use the following XPath:E.g, the following XSLT fragment show how to print ZipLog and Charge of preceding node only for those with Charge=’true’: