0

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

<?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>,所以我想删除这个父子元素并制作一个单元素<mbean>all parent elements and child elements</mbean>文件。

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

这是我期待的最终输出:

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

2 回答 2

1

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

<?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>
于 2013-08-26T15:42:58.297 回答
0

您可以更改路径。首先写父节点,然后是没有父亲的孩子。

<?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>
于 2013-08-22T21:01:45.570 回答