I have this XML and I want to add a new element name with a hard-coded value. How can I achieve it?
XML:
<m2:InvokeWebService xmlns:m2="http://www.w3.org/2001/XMLSchema-instance">
<m2:request>
<m2:action>ADD</m2:action>
<m2:commonDetails>
<m2:needSupport>Y</m2:needSupport>
</m2:commonDetails>
<m2:custDetails>
<m2:name>Tony,Hawk</m2:name>
<m2:accountNumber>23232423566</m2:accountNumber>
<m2:sensitiveCustomer>Y</m2:sensitiveCustomer>
</m2:custDetails>
</m2:request>
</m2:InvokeWebService>
Desired Output
<?xml version="1.0" encoding="UTF-8"?><m2:InvokeWebService xmlns:m2="http://www.w3.org/2001/XMLSchema-instance">
<m2:request>
<m2:action>ADD</m2:action>
<m2:commonDetails>
<m2:needSupport>Y</m2:needSupport>
</m2:commonDetails>
<m2:overrideScriptName>NewScript</m2:overrideScriptName>
<m2:custDetails>
<m2:name>Tony,Hawk</m2:name>
<m2:accountNumber>23232423566</m2:accountNumber>
<m2:sensitiveCustomer>Y</m2:sensitiveCustomer>
</m2:custDetails>
</m2:request>
</m2:InvokeWebService>
XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m2="http://www.w3.org/2001/XMLSchema-instance">
<!--<xsl:output method="xml" encoding="utf-8" indent="yes"/>-->
<!-- Identity template : copy all text nodes, elements and attributes -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<m2:overrideScriptName>CM-PrMtActMg</m2:overrideScriptName>
</xsl:stylesheet>
New Element to be added right before the custDetails
<m2:overrideScriptName>NewScript</m2:overrideScriptName>
CodePudding user response:
Please try the following solution.
It is using a so called identity transform pattern.
Input XML
<m2:InvokeWebService xmlns:m2="http://www.w3.org/2001/XMLSchema-instance">
<m2:request>
<m2:action>ADD</m2:action>
<m2:commonDetails>
<m2:needSupport>Y</m2:needSupport>
</m2:commonDetails>
<m2:custDetails>
<m2:name>Tony,Hawk</m2:name>
<m2:accountNumber>23232423566</m2:accountNumber>
<m2:sensitiveCustomer>Y</m2:sensitiveCustomer>
</m2:custDetails>
</m2:request>
</m2:InvokeWebService>
XSLT 1.0
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m2="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="m2:custDetails">
<m2:overrideScriptName>NewScript</m2:overrideScriptName>
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
Output XML
<?xml version='1.0' encoding='utf-8' ?>
<m2:InvokeWebService xmlns:m2="http://www.w3.org/2001/XMLSchema-instance">
<m2:request>
<m2:action>ADD</m2:action>
<m2:commonDetails>
<m2:needSupport>Y</m2:needSupport>
</m2:commonDetails>
<m2:overrideScriptName>NewScript</m2:overrideScriptName>
<m2:custDetails>
<m2:name>Tony,Hawk</m2:name>
<m2:accountNumber>23232423566</m2:accountNumber>
<m2:sensitiveCustomer>Y</m2:sensitiveCustomer>
</m2:custDetails>
</m2:request>
</m2:InvokeWebService>