Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT replace value

Tags:

xml

xslt

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.

like image 826
Duy Tran Avatar asked Jul 10 '13 00:07

Duy Tran


People also ask

How do you replace in XSLT?

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.

How do you use variables in XSLT?

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!

What is Tokenize in XSLT?

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.

What is normalize space in XSLT?

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.


1 Answers

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.

like image 129
harpo Avatar answered Nov 16 '22 04:11

harpo