mitchpau Posted February 8, 2011 Share Posted February 8, 2011 Hi,I have a piece of XML and am looking to change one attribute value if certain conditons are met, but although I can get my xsl to meet the criteria, the subsequent change of value to an existing element is not working. The XML looks like this:<tradeHeader> <otherPartyReference href="AAAAAA"/> <partyRole> <partyReference href="Y123"/> <role>Taker</role> </partyRole> <partyRole> <partyReference href="X123"/> <role>Maker</role> </partyRole></tradeHeader>I want to be able to say:if (partyRole/Taker and partyReference href=Y123) and otherPartyReference href="AAAAAA" then set otherPartyReference href="BBBBBB" otherwise don't change anything.Can anyone help?CheersMitch Link to comment Share on other sites More sharing options...
boen_robot Posted February 8, 2011 Share Posted February 8, 2011 I think the following should work: <xsl:templace match="tradeHeader[string(partyRole/role) = 'Taker']/otherPartyReference[@href = 'AAAAAA']/@href"> <xsl:copy>BBBBBB</xsl:copy></xsl:template><xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates/> <xsl:copy></xsl:template> The idea is to gather all nodes you want replaced in a single match expression for a template, and copy the rest. In this case, you also want to copy the attribute itself, but with a new value, which is why <xsl:copy> was also used there. Link to comment Share on other sites More sharing options...
mitchpau Posted February 8, 2011 Author Share Posted February 8, 2011 Thanks! Getting closer perhaps but the output now looks like this, for example, <otherPartyReference/> <partyRole> <partyReference/> <role>Taker</role> </partyRole>so the otherPartyReference is now blank and the href attribute has gone missing from the partyReferenceAny ideas?CheersMitch Link to comment Share on other sites More sharing options...
boen_robot Posted February 8, 2011 Share Posted February 8, 2011 I don't know why I remember attributes were also matched... maybe I've confused this with cases where I "manually" executed such templates.Well, here's one that this time I tested and I'm sure it works: <xsl:template match="tradeHeader[string(partyRole/role) = 'Taker']/otherPartyReference[@href = 'AAAAAA']"> <xsl:copy> <xsl:copy-of select="@*[name(.)!='href']" /> <xsl:attribute name="href">BBBBBB</xsl:attribute> <xsl:apply-templates/> </xsl:copy> </xsl:template> <xsl:template match="*"> <xsl:copy> <xsl:copy-of select="@*" /> <xsl:apply-templates/> </xsl:copy> </xsl:template> Same idea, only now the unmodified attributes are copied "manually". Link to comment Share on other sites More sharing options...
mitchpau Posted February 8, 2011 Author Share Posted February 8, 2011 WOW, thats great! , thanksFinal question, in my original post I also need to check that partyReference href=Y123 AS WELL AS partyRole/role = 'Taker'Can you add? Link to comment Share on other sites More sharing options...
boen_robot Posted February 8, 2011 Share Posted February 8, 2011 Let me try not to spoil you by giving you copy&paste stuff all the time .It's on the same premise as the "Taker" check, in the same predicate, and it has "and" as a separator between it and the existing check. Ideas? Link to comment Share on other sites More sharing options...
mitchpau Posted February 8, 2011 Author Share Posted February 8, 2011 I'll give it a go :-) Link to comment Share on other sites More sharing options...
mitchpau Posted February 9, 2011 Author Share Posted February 9, 2011 Just to round off this thread, I now have it working with all criteria <xsl:template match="tradeHeader[string(partyRole/role) = 'Taker' and partyRole/partyReference/@href = 'A123']/otherPartyReference[@href = 'AAAAAA']">Thanks for your superb helpIf you are ever in London, beer is on me Link to comment Share on other sites More sharing options...
mitchpau Posted February 9, 2011 Author Share Posted February 9, 2011 scrap that:-) more tinkering required I think Link to comment Share on other sites More sharing options...
boen_robot Posted February 9, 2011 Share Posted February 9, 2011 Hint: you're missing a string() there. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.