从xml文件中删除重复项使用xslt

时间:2013-08-05 13:24:39

标签: xml replace xslt-1.0 mbeans

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

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

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

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

的xpath

对不起,这是如此粗略,但就像我说的,这些只是我没有测试过的想法。我希望这至少有一点帮助,而且我不会完全走错方向。