根据属性值使用 xslt 合并父属性和子属性 [英] merge parent and child attributes using xslt based on attribute values

查看:72
本文介绍了根据属性值使用 xslt 合并父属性和子属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下标记的 xml 文件,我知道这个文件的格式不正确,但文件就是这样.

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>

如果您观察该文件,它包含一个名为 <mbean> 的子元素,在父元素 <mbean> 中,所以我想删除这个父元素-child 并制作一个单一元素 所有父元素和子元素 文件.

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.

我在 stackoverflow 中搜索了几个问题,但看起来它们与我现在遇到的问题并不完全相关.有人可以通过 xslt 1.0 或 2.0 给出如何使用此解决方案的想法.

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>

推荐答案

就像我在上一个答案中评论的那样,简单地更改 XPATH.我希望你服务.

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>

这篇关于根据属性值使用 xslt 合并父属性和子属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆