0

Below is the XML Input payload. I'm looking for an xml output should have "type" element inside the each Address node. Below is the incoming request XML

<rsp:response xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rsp="rsp.com/employee/Response/v30"
xmlns:res="res.com/Member/details/v1"
xmlns:resp="resp.com/details/v1">
        <res:employee>
            <resp:Employee>
                <resp:FirstName>abc</resp:FirstName>
                <resp:middleName></resp:middleName>
                <resp:details>
                    <resp:Details>
                        <resp:type>postal</resp:type>  
                        <resp:Addresses>
                            <resp:Address>
                                <resp:country>XYZ</resp:country>
                            </resp:Address>
                        </resp:Addresses>
                    </resp:Details>
                    <resp:Details>
                        <resp:type>ofc</resp:type> 
                        <resp:Addresses>
                            <resp:Address>
                                <resp:country>XYZ</resp:country>
                            </resp:Address>
                        </resp:Addresses>
                    </resp:Details>
                </resp:details>
            </resp:Employee>
        </res:employee>

</rsp:response>

Here is the XSLT used and it is not giving desired output. Using this XSLT all "type" elements is reflecting in each address block.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"    
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rsp="rsp.com/employee/Response/v30"
xmlns:res="res.com/Member/details/v1"
xmlns:resp="resp.com/details/v1"
version="2.0">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>  
<xsl:template match="node() | @*">
    <xsl:copy>
        <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
</xsl:template>  
<xsl:template match="//*[local-name()='response']/*[local-name()='employee']/*[local-name()='Employee']/*[local-name()='details']/*[local-name()='Details']/*[local-name()='Addresses']/*[local-name()='Address']">
    <xsl:copy>
        <xsl:apply-templates/>            
        <xsl:for-each select="//*[local-name()='response']/*[local-name()='employee']/*[local-name()='Employee']/*[local-name()='details']/*[local-name()='Details']/*[local-name()='type']">
            <xsl:copy-of select="."/>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>
<xsl:template match="//*[local-name()='response']/*[local-name()='employee']/*[local-name()='Employee']/*[local-name()='details']/*[local-name()='Details']/*[local-name()='type']"/>

</xsl:stylesheet>

Desired Output XML

<rsp:response
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rsp="rsp.com/employee/Response/v30"
xmlns:res="res.com/Member/details/v1"
xmlns:resp="resp.com/details/v1">
<res:employee>
    <resp:Employee>
        <resp:FirstName>abc</resp:FirstName>
        <resp:middleName/>
        <resp:details>
            <resp:Details>
                <resp:Addresses>
                    <resp:Address>
                        <resp:country>XYZ</resp:country>
                        <resp:type>postal</resp:type>
                    </resp:Address>
                </resp:Addresses>
            </resp:Details>
            <resp:Details>
                <resp:Addresses>
                    <resp:Address>
                        <resp:country>XYZ</resp:country>
                        <resp:type>ofc</resp:type>
                    </resp:Address>
                </resp:Addresses>
            </resp:Details>
        </resp:details>
    </resp:Employee>
</res:employee>

</rsp:response>

6
  • Please ask a specific question about a difficulty you encountered when trying to accomplish this. Otherwise it looks like you're just looking for someone to write your code for you. -- Note that the "XML" you have posted is not well-formed: you cannot use a prefix without a namespace declaration. Commented Jul 29, 2021 at 4:58
  • also, please share your expected result sample. Commented Jul 29, 2021 at 8:42
  • @sspsujit: Thanks for responding. I have attached the complete info including the desired output. Commented Jul 30, 2021 at 1:42
  • @michael.hor257k: Thanks Michael for responding. I have provided complete xml with namespaces along with xslt. Commented Jul 30, 2021 at 1:43
  • @michael.hor257k: Thanks for the response. XSLT is working as expected. Commented Aug 1, 2021 at 23:54

1 Answer 1

1

Try it this way:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:resp="resp.com/details/v1">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="resp:Address">
    <xsl:copy>
        <xsl:apply-templates/>
        <xsl:copy-of select="../../resp:type"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="resp:type"/>

</xsl:stylesheet>

Re your attempt:

  1. There should never be a need to use a hack like *[local-name()='type'];

  2. You should find out what // means.

Sign up to request clarification or add additional context in comments.

1 Comment

This XSLT is working as expected. Thanks a ton.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.