kumar
kumar

Reputation: 389

merge parent and child attributes using xslt based on attribute values

I have an xml file which contains the following markup, I know the format of this file is not correct but that's how the file is.

<?xml version="1.0" encoding="UTF-8"?>
<mbean code="org.book.mybooks"
   name="mycompany.props.jndi:name=mybookprops">
    <attribute name="CombineProps" 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="books/mybooks/cartoon/comics">
                <jndi:value type="java.lang.String">
                        @Value@
                </jndi:value>
            </jndi:binding>
            <jndi:binding name="abc/ebooks/onebook/action">
                <jndi:value type="java.lang.String">
                        @Value@
                </jndi:value>
            </jndi:binding>
            <jndi:binding name="abc/ibooks/twobook/romance">
                <jndi:value type="java.lang.String">
                        @Value@
                </jndi:value>
            </jndi:binding>

        </jndi:bindings>
    </attribute>
    <mbean code="org.book.mybooks"
          name="mycompany.props.jndi:name=mybookprops">
        <attribute name="CombineProps" 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="books/mybooks/cartoon/comics">
                    <jndi:value type="java.lang.String">
                        @New_Value@
                    </jndi:value>
                </jndi:binding>
                <jndi:binding name="abc/ebooks/onebook/action">
                    <jndi:value type="java.lang.String">
                        @New_Value@
                    </jndi:value>
                </jndi:binding>
                <jndi:binding name="books/new/books/cartoon">
                    <jndi:value type="java.lang.String">
                        @Value@
                    </jndi:value>
                </jndi:binding>
            </jndi:bindings>
        </attribute>
    </mbean>
</mbean>

If you observe the file, it contain a child element called <mbean> with in a parent element <mbean>, so i want to remove this parent-child and make a one single element <mbean>all parent elements and child elements</mbean> file.

I have searched few questions in stackoverflow but it looks like they are not exactly related to the problem that i am having right now. Can someone give an idea how to work on this solution using either by xslt 1.0 or 2.0.

This is the final output that i am expecting:

<?xml version="1.0" encoding="UTF-8"?>
<mbean code="org.book.mybooks"
   name="mycompany.props.jndi:name=mybookprops">
    <attribute name="CombineProps" 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="books/mybooks/cartoon/comics">
                <jndi:value type="java.lang.String">
                        @New_Value@
                </jndi:value>
            </jndi:binding>
            <jndi:binding name="abc/ebooks/onebook/action">
                <jndi:value type="java.lang.String">
                        @New_Value@
                </jndi:value>
            </jndi:binding>
            <jndi:binding name="abc/ibooks/twobook/romance">
                <jndi:value type="java.lang.String">
                        @Value@
                </jndi:value>
            </jndi:binding>
            <jndi:binding name="books/new/books/cartoon">
                <jndi:value type="java.lang.String">
                        @Value@
                </jndi:value>
            </jndi:binding>
        </jndi:bindings>
    </attribute>
</mbean>

Upvotes: 0

Views: 748

Answers (2)

dperez
dperez

Reputation: 101

Simple change the XPATH as I commented in the previous answer. I hope you serve.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:jndi="urn:jboss:jndi-binding-service:1.0"  >
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes" /> 
    <xsl:template match="mbean/mbean">  
    <mbean code="org.book.mybooks"
   name="mycompany.props.jndi:name=mybookprops">
    <attribute name="CombineProps" 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">    
        <xsl:copy-of select="attribute/jndi:bindings/jndi:binding"/>
        <xsl:call-template name="Mbean">
          <xsl:with-param name="bindings" select="attribute/jndi:bindings/jndi:binding"/>
        </xsl:call-template>
        </jndi:bindings>
        </attribute>           
     </mbean>   
    </xsl:template>   
    <xsl:template name="Mbean">
       <xsl:param name="bindings"/>
       <xsl:for-each select="/mbean/attribute/jndi:bindings/jndi:binding">
                <xsl:variable name="currentBinding" select="self::node()"/>  
                <xsl:if test="not(@name[. = $bindings/@name])">
                          <xsl:copy-of select="self::node()"/>
                </xsl:if>                        
       </xsl:for-each>
    </xsl:template>
   <xsl:template match="text()"></xsl:template>
</xsl:stylesheet>

Upvotes: 1

dperez
dperez

Reputation: 101

You can change paths. First write parent nodes and then the children who does not have the dad.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:jndi="urn:jboss:jndi-binding-service:1.0">

  <!--Put missing nodes as mbean-->

    <xsl:template match="mbean">          
        <xsl:copy-of select="attribute/jndi:bindings/jndi:binding"/>
        <xsl:call-template name="childMbean">
          <xsl:with-param name="bindings" select="attribute/jndi:bindings/jndi:binding"/>
        </xsl:call-template>
    </xsl:template>    

    <xsl:template name="childMbean">
       <xsl:param name="bindings"/>
       <xsl:for-each select="mbean/attribute/jndi:bindings/jndi:binding">
                <xsl:variable name="currentBinding" select="self::node()"/>  
                <xsl:if test="not(@name[. = $bindings/@name])">
                          <xsl:copy-of select="self::node()"/>
                </xsl:if>                        
       </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

Upvotes: 0

Related Questions