这是一个示例,其中我有两个<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>
答案 0 :(得分:2)
您需要使用 Muenchian Method 删除重复项
<?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 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>
<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])]
对不起,这是如此粗略,但就像我说的,这些只是我没有测试过的想法。我希望这至少有一点帮助,而且我不会完全走错方向。