Question

Here is an example where i have two <mbean> sections with same name, but the properties in these sections are diffrent, I would like to merge these two <mbean> sections and duplicate propeties has to replace with the latest one that is added to bottom of <mbean> section.

        <mbean code="org.jboss.naming.JNDIBindingServiceMgr"
        name="abc.myconfig.jndi:name=myconfigAppPartitionJNDI">
        <attribute name="BindingsConfig" serialDataType="jbxb">
            <jndi:bindings
                xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:jndi="urn:jboss:jndi-binding-service:1.0"
                xs:schemaLocation="urn:jboss:jndi-binding-service:1.0 resource:jndi-binding-service_1_0.xsd">                   
                <jndi:binding
                    name="myabc/myconfig/myservice/myabcservice">
                    <jndi:value type="java.lang.String">
                        old-value
                    </jndi:value>
                </jndi:binding>
                <jndi:binding name="myabc/myconfig/myabcdefService/myabcdefServiceUrl">
                      <jndi:value type="java.lang.String">
                            @myabcdefService.myabcdefServiceUrl@
                      </jndi:value>
                </jndi:binding>
            </jndi:bindings> 
        </attribute>
    </mbean>
    <mbean code="org.jboss.naming.JNDIBindingServiceMgr"
        name="abc.myconfig.jndi:name=myconfigAppPartitionJNDI">
        <attribute name="BindingsConfig" serialDataType="jbxb">
            <jndi:bindings
                xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:jndi="urn:jboss:jndi-binding-service:1.0"
                xs:schemaLocation="urn:jboss:jndi-binding-service:1.0 resource:jndi-binding-service_1_0.xsd">                   
                <jndi:binding
                    name="myabc/myconfig/myservice/myabcservice">
                    <jndi:value type="java.lang.String">
                        new-value
                    </jndi:value>
                </jndi:binding>
                <jndi:binding name="myabc/myconfig/myabcdefService/myabcdefServiceUrl">
                      <jndi:value type="java.lang.String">
                            @myabcdefService.myabcdefServiceUrl@
                      </jndi:value>
                </jndi:binding>
                <jndi:binding name="myabc/myconfig/myabcdefService/myabcFileNet">
                      <jndi:value type="java.lang.String">
                            @myabcdefService.myabcFileNet@
                      </jndi:value>
                </jndi:binding>
            </jndi:bindings> 
        </attribute>
    </mbean>

Expected output is:

                <mbean code="org.jboss.naming.JNDIBindingServiceMgr"
        name="abc.myconfig.jndi:name=myconfigAppPartitionJNDI">
        <attribute name="BindingsConfig" serialDataType="jbxb">
            <jndi:bindings
                xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:jndi="urn:jboss:jndi-binding-service:1.0"
                xs:schemaLocation="urn:jboss:jndi-binding-service:1.0 resource:jndi-binding-service_1_0.xsd">                   
                <jndi:binding name="myabc/myconfig/myabcdefService/myabcdefServiceUrl">
                      <jndi:value type="java.lang.String">
                            @myabcdefService.myabcdefServiceUrl@
                      </jndi:value>
                </jndi:binding>
                <jndi:binding
                    name="myabc/myconfig/myservice/myabcservice">
                    <jndi:value type="java.lang.String">
                        new-value
                    </jndi:value>
                </jndi:binding>
                <jndi:binding name="myabc/myconfig/myabcdefService/myabcFileNet">
                      <jndi:value type="java.lang.String">
                            @myabcdefService.myabcFileNet@
                      </jndi:value>
                </jndi:binding>
            </jndi:bindings> 
        </attribute>
    </mbean>
Was it helpful?

Solution

You need to use Muenchian Method for removing duplicates

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:key name="mbeanName" match="//mbean/@name" use="."/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="mbean[not(generate-id(@name) = generate-id(key('mbeanName', @name)[1]))]"/>
</xsl:stylesheet>

XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <mbean code="org.jboss.naming.JNDIBindingServiceMgr" name="abc.myconfig.jndi:name=myconfigAppPartitionJNDI">
        <attribute name="BindingsConfig" serialDataType="jbxb">
            <jndi:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xmlns:jndi="urn:jboss:jndi-binding-service:1.0" xs:schemaLocation="urn:jboss:jndi-binding-service:1.0 resource:jndi-binding-service_1_0.xsd">
                <jndi:binding name="myabc/myconfig/myservice/myabcservice">
                    <jndi:value type="java.lang.String">
                        old-value
                    </jndi:value>
                </jndi:binding>
                <jndi:binding name="myabc/myconfig/myabcdefService/myabcdefServiceUrl">
                    <jndi:value type="java.lang.String">
                            @myabcdefService.myabcdefServiceUrl@
                      </jndi:value>
                </jndi:binding>
            </jndi:bindings>
        </attribute>
    </mbean>
    <mbean code="org.jboss.naming.JNDIBindingServiceMgr" name="abc.myconfig.jndi:name=myconfigAppPartitionJNDI">
        <attribute name="BindingsConfig" serialDataType="jbxb">
            <jndi:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xmlns:jndi="urn:jboss:jndi-binding-service:1.0" xs:schemaLocation="urn:jboss:jndi-binding-service:1.0 resource:jndi-binding-service_1_0.xsd">
                <jndi:binding name="myabc/myconfig/myservice/myabcservice">
                    <jndi:value type="java.lang.String">
                        new-value
                    </jndi:value>
                </jndi:binding>
                <jndi:binding name="myabc/myconfig/myabcdefService/myabcdefServiceUrl">
                    <jndi:value type="java.lang.String">
                            @myabcdefService.myabcdefServiceUrl@
                      </jndi:value>
                </jndi:binding>
                <jndi:binding name="myabc/myconfig/myabcdefService/myabcFileNet">
                    <jndi:value type="java.lang.String">
                            @myabcdefService.myabcFileNet@
                      </jndi:value>
                </jndi:binding>
            </jndi:bindings>
        </attribute>
    </mbean>
</root>

OUTPUT:

<root>
    <mbean code="org.jboss.naming.JNDIBindingServiceMgr" name="abc.myconfig.jndi:name=myconfigAppPartitionJNDI">
        <attribute name="BindingsConfig" serialDataType="jbxb">
            <jndi:bindings xmlns:jndi="urn:jboss:jndi-binding-service:1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:schemaLocation="urn:jboss:jndi-binding-service:1.0 resource:jndi-binding-service_1_0.xsd">
                <jndi:binding name="myabc/myconfig/myservice/myabcservice">
                    <jndi:value type="java.lang.String">
                        old-value
                    </jndi:value>
                </jndi:binding>
                <jndi:binding name="myabc/myconfig/myabcdefService/myabcdefServiceUrl">
                    <jndi:value type="java.lang.String">
                            @myabcdefService.myabcdefServiceUrl@
                      </jndi:value>
                </jndi:binding>
            </jndi:bindings>
        </attribute>
    </mbean>
</root>

UPDATED XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:key name="mbeanName" match="//mbean/@name" use="."/>
    <xsl:key name="mbeanCount" match="//mbean[generate-id(@name) = generate-id(key('mbeanName', @name)[1])]" use="count(.)"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="mbean[count(. | key('mbeanCount', /mbean/@name))]" />
</xsl:stylesheet>

OTHER TIPS

The mbean template in Siva Charan's answer eliminates mbean elements where the @name is duplicated, so that part you want to keep.

I was thinking that maybe you could add a template matching mbean that gathered up all the attribute nodes where the parent node has the same name. So underneath the mbean template, you would be selecting an xpath something like this ../mbean/attribute[../@name = current()/@name] and let's say you set that to a variable $attributes. Then you hard code <attributes> and <jndi:bindings>, and then output all $attributes/jndi:bindings/jndi:binding such that jndi:value is unique. So for that, I guess you'd set a key matching jndi:value, and then make an xpath something like jndi:binding[generate-id(jndi:value) = generate-id(key('jndivals', jndi:value)[1])]

Sorry this is so sketchy, but like I said, these are just thoughts that I haven't tested. I hope that helps at least a little bit, and that I'm not going in completely the wrong direction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top