0

这是一个示例,其中我有两个<mbean>同名的部分,但这些部分中的属性不同,我想合并这两个<mbean>部分,并且重复的属性必须替换为添加到<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">
                        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>

预期输出为:

                <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>
4

2 回答 2

2

您需要使用Muenchian Method删除重复项

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>

输出:

<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>

更新的 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>
于 2013-08-05T13:53:37.153 回答
0

Siva Charan 的答案中的 mbean 模板消除了 @name 重复的 mbean 元素,因此您要保留该部分。

我在想也许你可以添加一个模板匹配 mbean,它收集了父节点具有相同名称的所有属性节点。因此,在 mbean 模板下,您将选择类似这样的 xpath ../mbean/attribute[../@name = current()/@name],假设您将其设置为 variable $attributes。然后你硬编码<attributes>and <jndi:bindings>,然后输出所有$attributes/jndi:bindings/jndi:binding唯一jndi:value的。所以为此,我猜你会设置一个匹配 jndi:value 的键,然后创建一个类似的 xpathjndi:binding[generate-id(jndi:value) = generate-id(key('jndivals', jndi:value)[1])]

抱歉,这太粗略了,但就像我说的,这些只是我没有测试过的想法。我希望这至少会有所帮助,并且我不会完全走错方向。

于 2013-08-05T17:05:01.563 回答