View Single Post
  #1 (permalink)  
Old May 24th, 2018, 04:56 AM
Neal Neal is offline
Friend of Wrox
Points: 696, Level: 9
Points: 696, Level: 9 Points: 696, Level: 9 Points: 696, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: Ennis, Clare, Ireland.
Posts: 129
Thanks: 10
Thanked 0 Times in 0 Posts
Default Copy ALL elements except one

Hi,
I have an XML file that I need to update using XSLT. The update is on 1 field only (DocumentDestinationPartner). The rest of the XML should remain the same.

Original XML file

HTML Code:
<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
	<Header>
		<MessageId>ABCD</MessageId>
		<Action>http://blah/</Action>
		<DocumentDestinationDivision>CBA</DocumentDestinationDivision>
		<DocumentDestinationPartner>CBA</DocumentDestinationPartner>
		<DocumentSourceDivision>TEST</DocumentSourceDivision>
		<DocumentSourcePartner>AX12PPR</DocumentSourcePartner>
		<DocumentStandard>XXXX</DocumentStandard>
		<DocumentTimeStamp>20180522133623</DocumentTimeStamp>
		<DocumentType>Document1</DocumentType>
		<DocumentUniqueId>00012</DocumentUniqueId>
	</Header>
	<Body>
		<MessageParts xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
			...
			...
		</MessageParts>
	</Body>
</Envelope>

I have tried updating using the following XSLT technique (I ‘borrowed’ from the web). But it doesn’t update the specific field.


XSLT file

HTML Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<!--<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>-->
	<xsl:output method="xml" version="1.0" 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>
    <!-- -->
    <!-- 1. Append '#D' or 'D'-->
	<xsl:template match="DocumentDestinationPartner">
		<DocumentDestinationPartner>
			<xsl:choose>
				<xsl:when test="contains(//DocumentDestinationPartner, '#D')">
					<xsl:value-of select="concat(//DocumentDestinationPartner, 'D')"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="concat(//DocumentDestinationPartner, '#D')"/>
				</xsl:otherwise>
			</xsl:choose>
		</DocumentDestinationPartner>
	</xsl:template>
	<!-- -->
    <xsl:template match="text()">
		<xsl:value-of select="normalize-space()" />
	</xsl:template>
	<!-- -->
</xsl:stylesheet>

In addition, the following attribute for the 'MessageParts' element is removed from the output (NOTE: it is the same as the Envelope attribute) which I wasn't expecting:

HTML Code:
xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message"

Can anyone help please?


Thanks in advance,
__________________
Neal

A Northern Soul

Last edited by Neal; May 24th, 2018 at 05:08 AM..
Reply With Quote