osfar
osfar

Reputation: 413

replace tag value recusersivly in xml payload

i have a xml payload that conatins many childs , and there is tag

<DomainName>value</DomainName>
<DOMAIN_NAME>value</DOMAIN_NAME>

what i want to do is to traverse the whole xml payload whith all i'ts childe and replace them with new value with xsl code

for example this is a sample payload

<TransmissionHeader xmlns:tran="http://xmlns.oracle.com/apps/otm/TransmissionService" xmlns="">
            <Version>20b</Version>
            <TransmissionCreateDt>
                <GLogDate>20200819124057</GLogDate>
                <TZId>UTC</TZId>
                <TZOffset>+00:00</TZOffset>
            </TransmissionCreateDt>
            <TransactionCount>1</TransactionCount>
            <SenderHostName>https://xxx</SenderHostName>
            <SenderSystemID>https:xxx</SenderSystemID>
            <UserName>OOO</UserName>
            <SenderTransmissionNo>404836</SenderTransmissionNo>
            <ReferenceTransmissionNo>0</ReferenceTransmissionNo>
            <GLogXMLElementName>PlannedShipment</GLogXMLElementName>
            <NotifyInfo>
                <ContactGid>
                    <Gid>
                        <DomainName>OOO</DomainName>
                        <Xid>SYS</Xid>
                    </Gid>
                </ContactGid>
                <ExternalSystemGid>
                    <Gid>
                        <DOMAIN_NAME>OOO</DOMAIN_NAME>
                        <Xid>IOT_SYSTEM</Xid>
                    </Gid>
                </ExternalSystemGid>
            </NotifyInfo>
        </TransmissionHeader>

it should replace the tags with the new values . how to achieve that ?

 <xsl:template match="/" xml:id="id_11">
      <nstrgmpr:POST xml:id="id_12">
         <nstrgmpr:request-wrapper xml:id="id_26">
            <ns26:input xml:id="id_27">
               <xsl:copy-of xml:id="id_28" select=" /nssrcmpr:publish/Transmission/TransmissionHeader"/>

            </ns26:input>
            <ns26:newDomain xml:id="id_29">d1</ns26:newDomain>
         </nstrgmpr:request-wrapper>
      </nstrgmpr:POST>
   </xsl:template>

Upvotes: 0

Views: 62

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117073

Applying the following stylesheet to your input:

XSLT 1.0

<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"/>
<xsl:strip-space elements="*"/>

<xsl:param name="new-domain-name">new.domain.com</xsl:param>

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

<xsl:template match="DomainName | DOMAIN_NAME">
    <xsl:copy>
        <xsl:value-of select="$new-domain-name"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

will produce:

Result

<?xml version="1.0" encoding="UTF-8"?>
<TransmissionHeader xmlns:tran="http://xmlns.oracle.com/apps/otm/TransmissionService" xmlns="">
  <Version>20b</Version>
  <TransmissionCreateDt>
    <GLogDate>20200819124057</GLogDate>
    <TZId>UTC</TZId>
    <TZOffset>+00:00</TZOffset>
  </TransmissionCreateDt>
  <TransactionCount>1</TransactionCount>
  <SenderHostName>https://xxx</SenderHostName>
  <SenderSystemID>https:xxx</SenderSystemID>
  <UserName>OOO</UserName>
  <SenderTransmissionNo>404836</SenderTransmissionNo>
  <ReferenceTransmissionNo>0</ReferenceTransmissionNo>
  <GLogXMLElementName>PlannedShipment</GLogXMLElementName>
  <NotifyInfo>
    <ContactGid>
      <Gid>
        <DomainName>new.domain.com</DomainName>
        <Xid>SYS</Xid>
      </Gid>
    </ContactGid>
    <ExternalSystemGid>
      <Gid>
        <DOMAIN_NAME>new.domain.com</DOMAIN_NAME>
        <Xid>IOT_SYSTEM</Xid>
      </Gid>
    </ExternalSystemGid>
  </NotifyInfo>
</TransmissionHeader>

Upvotes: 1

Related Questions