I have an XML like this
<?xml version="1.0" encoding="UTF-8"?>
<OMDefault xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<PrintDollarsAndCents>X</PrintDollarsAndCents>
<MailAddrLine1>Add1</MailAddrLine1>
<MailAddrLine2>Add2</MailAddrLine2>
</OMDefault>
I would like to have an XSLT to transform the XML to this
<?xml version="1.0" encoding="UTF-8"?>
<OMDefault xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<PrintDollarsAndCents>Y</PrintDollarsAndCents>
<MailAddrLine1>Add1</MailAddrLine1>
<MailAddrLine2>Add2</MailAddrLine2>
</OMDefault>
Please notice the 'X' gets transformed to 'Y' if the attribute is PrintDollarsAndCents and its value is 'X' Could someone please help me with this? As I am very new to this XSLT thing.
Thank you in advance.
XSLT replace is deterministic and does string manipulation that replaces a sequence of characters defined inside a string that matches an expression. In simple terms, it does string substitution in the specified place by replacing any substrings. Fn: replace function is not available in XSLT1.
XSLT <xsl:variable> The <xsl:variable> element is used to declare a local or global variable. Note: The variable is global if it's declared as a top-level element, and local if it's declared within a template. Note: Once you have set a variable's value, you cannot change or modify that value!
XSLT tokenize is defined to break a declared string with one more delimiter character by treating each token as a node having <token> element and a part of XSLT2.
The normalize-space() function It does three things: It removes all leading spaces. It removes all trailing spaces. It replaces any group of consecutive whitespace characters with a single space.
Basically you want an identity transform, with override rules.
The following transform
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="PrintDollarsAndCents/text()[.='X']">Y</xsl:template>
</xsl:stylesheet>
applied to your input, produces the result:
<OMDefault xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<PrintDollarsAndCents>Y</PrintDollarsAndCents>
<MailAddrLine1>Add1</MailAddrLine1>
<MailAddrLine2>Add2</MailAddrLine2>
</OMDefault>
The first template is an identity transform, which copies the input document exactly.
The second template overrides text nodes with a value of X
that are children of a PrintDollarsAndCents
template. Note that it emits the value Y
instead of its actual content.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With