Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Support Questions

Find answers, ask questions, and share your expertise
Announcements
Share your experience with Cloudera on G2 and get a $25 Amazon Gift card.
Hi, I'm CLEO! Something exciting is coming to the Community. Stay Tuned!

copying Parent XML tag value to child tag value

avatar
Expert Contributor

Hi Team , 

                Could you please help me in working with XML tag's in Nifi. I've a scenario where i need to look for the parent xml tag value in entire document, the corresponding child xml tag value should match. if it is not matching we should copy the parent xml tag value to the corresponding specified child tag.

here the parent tag is <OriginalQTY name=" 5.000">    and the child tag should match is <OriginalORDEREDPURCHASEQUANTITY>   So every time <OriginalORDEREDPURCHASEQUANTITY> should match <OriginalQTY> value in name.

 

<Document>
<PURCHPURCHASEORDERHEADERV2ENTITY>
<VENDORACCOUNT>XXXXX</VENDORACCOUNT>
<CURRENCYCODE>ZAR</CURRENCYCODE>
<OriginalQTY name=" 1.000">
<PURCHPURCHASEORDERLINEV2ENTITY>
<PURCHASEORDERNUMBER>PO-XXXXXXX</PURCHASEORDERNUMBER>
<LINENUMBER>0000000100</LINENUMBER>
<PURCHASEPRICE>???</PURCHASEPRICE>
<ITEMNUMBER>XXXXXXXXX</ITEMNUMBER>
<CONFIRMEDQUANTITY> 0.000</CONFIRMEDQUANTITY>
<OriginalORDEREDPURCHASEQUANTITY> 7.000</OriginalORDEREDPURCHASEQUANTITY>
</OriginalQTY>
<OriginalQTY name=" 5.000">
<PURCHPURCHASEORDERLINEV2ENTITY>
<PURCHASEORDERNUMBER>PO-XXXXXXXXX</PURCHASEORDERNUMBER>
<LINENUMBER>0000000100</LINENUMBER>
<PURCHASEPRICE>???</PURCHASEPRICE>
<ITEMNUMBER>A4634101302</ITEMNUMBER>
<CONFIRMEDQUANTITY> 0.000</CONFIRMEDQUANTITY>
<OriginalORDEREDPURCHASEQUANTITY> 7.000</OriginalORDEREDPURCHASEQUANTITY>
</OriginalQTY>
</PURCHPURCHASEORDERHEADERV2ENTITY>
</Document>

1 ACCEPTED SOLUTION

avatar
Super Guru

Hi,

Are you looking to copy the value from the parent tag name attribute "<OriginalQTY name=" 5.000">" to the value of the child tag "<OriginalORDEREDPURCHASEQUANTITY>" so that its matching all the time? If so you can try the TransformXML processor with the following script:

 

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
 <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Document/PURCHPURCHASEORDERHEADERV2ENTITY/OriginalQTY/PURCHPURCHASEORDERLINEV2ENTITY/OriginalORDEREDPURCHASEQUANTITY">
        <xsl:copy>
             <xsl:value-of select="../../@name"/>
        </xsl:copy>
    </xsl:template>
</xsl:transform>

If you find that helpful please accept solution. Thanks

View solution in original post

3 REPLIES 3

avatar
Expert Contributor

@mburgess Could you please help on this? thank you!!

avatar
Super Guru

Hi,

Are you looking to copy the value from the parent tag name attribute "<OriginalQTY name=" 5.000">" to the value of the child tag "<OriginalORDEREDPURCHASEQUANTITY>" so that its matching all the time? If so you can try the TransformXML processor with the following script:

 

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
 <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Document/PURCHPURCHASEORDERHEADERV2ENTITY/OriginalQTY/PURCHPURCHASEORDERLINEV2ENTITY/OriginalORDEREDPURCHASEQUANTITY">
        <xsl:copy>
             <xsl:value-of select="../../@name"/>
        </xsl:copy>
    </xsl:template>
</xsl:transform>

If you find that helpful please accept solution. Thanks

avatar
Expert Contributor

Yes that's the requirement.

Thank you so much @SAMSAL , it worked. perfectly, Could you please help me any sources to get used to these xml scripts.