0

I'm trying to follow the solution provided in this post to merge two XMLs, my issue is that with the namespace I'm not able to make it work, if I remove the namespace then it works fine. To handle the namespace, I've followed the solution provided in this post but still no luck.

input1.xml

<?xml version="1.0" encoding="utf-8"?>
<Template xsi:schemaLocation="Invoice_Summary_Template" Name="Invoice_Summary_Template" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="Invoice_Summary_Template">
    <Batches>
        <Collection>
            <Item>
                <Batch>
                    <Report Name="Invoice_Summary">
                        <Invoices>
                            <Invoice_Collection>
                                <Invoice Id="1">                                    
                                    <content>
                                        <misc>aaa</misc>
                                        <misc>bbb</misc>
                                    </content>                                    
                                </Invoice>
                            </Invoice_Collection>
                        </Invoices>
                    </Report>
                </Batch>                
            </Item>
        </Collection>
    </Batches>
</Template> 

input2.xml

<?xml version="1.0" encoding="utf-8"?>
<Template xsi:schemaLocation="Invoice_Summary_Template" Name="Invoice_Summary_Template" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="Invoice_Summary_Template">
    <Batches>
        <Collection>
            <Item>
                <Batch>
                    <Report Name="Invoice_Summary">
                        <Invoices>
                            <Invoice_Collection>
                                <Invoice Id="2">                                    
                                    <content>
                                        <misc>ccc</misc>                                        
                                    </content>
                                </Invoice>
                            </Invoice_Collection>
                        </Invoices>
                    </Report>
                </Batch>                
            </Item>
        </Collection>
    </Batches>
</Template> 

collection.xml

<?xml version="1.0" encoding="UTF-8"?>
<collection>
  <doc href="C:/test/input1.xml"/>
  <doc href="C:/test/input2.xml"/>
</collection>

Output:

<?xml version="1.0" encoding="utf-8"?>
<Template xsi:schemaLocation="Invoice_Summary_Template" Name="Invoice_Summary_Template" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="Invoice_Summary_Template">
    <Batches>
        <Collection>
            <Item>
                <Batch>
                    <Report Name="Invoice_Summary">
                        <Invoices>
                            <Invoice_Collection>
                                <Invoice Id="1">                                    
                                    <content>
                                        <misc>aaa</misc>
                                        <misc>bbb</misc>
                                    </content>                                    
                                </Invoice>
                            </Invoice_Collection>
                        </Invoices>
                    </Report>
                </Batch>                
            </Item>
            <Item>
                <Batch>
                    <Report Name="Invoice_Summary">
                        <Invoices>
                            <Invoice_Collection>
                                <Invoice Id="2">                                    
                                    <content>
                                        <misc>ccc</misc>                                        
                                    </content>
                                </Invoice>
                            </Invoice_Collection>
                        </Invoices>
                    </Report>
                </Batch>                
            </Item>
        </Collection>
    </Batches>
</Template> 

XSLT 2.0

<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xpath-default-namespace="Invoice_Summary_Template"
                exclude-result-prefixes="#all">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="collection">
    <Template xsi:schemaLocation="Invoice_Summary_Template" Name="Invoice_Summary_Template" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="Invoice_Summary_Template">
        <Batches>
            <Collection>
                <xsl:copy-of select="document(doc/@href)/*/Batches/Collection/Item"/>
            </Collection>      
        </Batches>
    </Template>
</xsl:template>

</xsl:stylesheet>

What changes do I need to make to accomplish this?

ayk
  • 7
  • 3
  • The problem is your collection.xml not being in the same namespace as the others. There are several options, but first if you could change that collection.xml to use the same namespace, everything should work just fine – Siebe Jongebloed Jul 20 '23 at 19:08
  • That worked! Please post an answer to give you the credit. – ayk Jul 20 '23 at 19:25

2 Answers2

1

The easy way:

Change your collection.xml to use the same namespace as the other two like this:

<?xml version="1.0" encoding="UTF-8"?>
<collection xmlns="Invoice_Summary_Template" >
  <doc href="C:/test/input1.xml"/>
  <doc href="C:/test/input2.xml"/>
</collection>
Siebe Jongebloed
  • 3,906
  • 2
  • 14
  • 19
1

The problem is indeed that the collection.xml document is not in the default namespace.

To address this issue in the XSLT stylesheet, without having to modify the input, you could do:

XSLT 2.0

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:inv="Invoice_Summary_Template"
exclude-result-prefixes="#all">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="collection">
    <Template xsi:schemaLocation="Invoice_Summary_Template" Name="Invoice_Summary_Template" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="Invoice_Summary_Template">
        <Batches>
            <Collection>
                <xsl:copy-of select="document(doc/@href)/*/inv:Batches/inv:Collection/inv:Item" />
            </Collection>      
        </Batches>
    </Template>
</xsl:template>

</xsl:stylesheet>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51