Jump to content

How to change attribute based on certain conditions


mitchpau
 Share

Recommended Posts

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

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

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

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

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...