Jump to content

Change tag value in XSL

Recommended Posts

Hello everyone, I am new to xsl and trying to adjust an existing template at work. I am trying to change the value of a tag via the xsl. Example: Is the value of the SC_DF_FIELD_2 field in xml is "inactive", I would like to to display as something like "retired". I saw some code that I have been trying to insert into the existsing document but I am unable to get it to work. When I look at the xml, it appears like below. <SC_DF_FIELD_2 display_name="Employee status" element_type="r"><![CDATA[inactive]]></SC_DF_FIELD_2> This was the code I received.<xsl:template match="A1[text() = '0']"><A1>AA</A1></xsl:template> I changed it to read <xsl:template match="SC_DF_FIELD_2[text() = 'inactive']"><SC_DF_FIELD_2>retired</SC_DF_FIELD_2></xsl:template> Is there something else I should be doing? Thanks for taking a look. Stephen

Link to post
Share on other sites

Since your source XML uses a CDATA, you may have to compare the whole text contents instead of just text(), i.e.

<xsl:template match="SC_DF_FIELD_2[. = 'inactive']"><SC_DF_FIELD_2>retired</SC_DF_FIELD_2></xsl:template>

Your XSLT should also contain a template that would define what to do for other nodes. Or at least templates that would eventually use xsl:apply-templates so that your template is matched.If you don't have other templates, and want to preserve all other nodes, you can use a template that just copies the node, and applies the inner templates (which will eventually lead to a match on your other template), like so:

<xsl:template match="*|@*|node()"><xsl:copy><xsl:apply-templates/></xsl:copy></xsl:template>

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...