2013-08-26 74 views
1

这是我的源文件看起来像:合并两个元素的值

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<book> 
    <mbean code="org.book.mybooks" name="mycompany.props.jndi:name=mybookprops"> 
     <attribute name="bookprops"> 
      [email protected]@ 
      [email protected]@ 
      [email protected]@ 
     </attribute> 
    </mbean> 
    <book> 
     <mbean code="org.book.mybooks" name="mycompany.props.jndi:name=mybookprops"> 
      <attribute name="bookprops"> 
      [email protected][email protected] 
      [email protected][email protected] 
      [email protected]@ 

      </attribute> 
     </mbean> 
    </book> 
</book> 

我希望得到两个属性合并成一个,并复制所有的匹配线,新的变量@[email protected]和复制所有其他不匹配的行到文件末尾。

这个问题与我之前发布的问题非常相似merge parent and child attributes using xslt based on attribute values唯一不同的是XML文件中内容的格式。

此基础上在上述网址提供的解决方案,我已经调整了我的XSL获得工作这个新的XML文件,这里是XSL文件:

<?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="book/book"> 
     <book> 
      <mbean code="org.book.mybooks" name="mycompany.props.jndi:name=mybookprops">  
       <attribute name="bookprops"> 
        <xsl:copy-of select="mbean/attribute/node()"/> 
        <xsl:call-template name="Mbean"> 
         <xsl:with-param name="bindings" select="book/mbean/attribute"/> 
        </xsl:call-template> 
       </attribute> 
      </mbean> 
     </book> 
    </xsl:template> 

    <xsl:template name="Mbean"> 
     <xsl:param name="bindings"/> 
     <xsl:for-each select="/book/mbean/attribute/node()"> 
      <xsl:variable name="currentBinding" select="self::node()"/> 
      <xsl:if test="not(node()[. = $bindings])"> 
       <xsl:copy-of select="self::node()"/> 
      </xsl:if>       
     </xsl:for-each> 
    </xsl:template> 
    <xsl:template match="text()"/> 
</xsl:stylesheet> 

但不知何故,我没能得到预期的结果,以下是我期待的预期的结果:

<book> 
    <mbean code="org.book.mybooks" name="mycompany.props.jndi:name=mybookprops"> 
     <attribute name="bookprops"> 
      [email protected][email protected] 
      [email protected][email protected] 
      [email protected]@ 
      [email protected]@ 
     </attribute> 
    </mbean> 
</book> 

回答

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" > 
    <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes" /> 
    <xsl:template match="book/book"> 
     <book> 
      <mbean code="org.book.mybooks" name="mycompany.props.jndi:name=mybookprops"> 
      <attribute name="bookprops">     
        <xsl:copy-of select="mbean/attribute/text()"/> 
        <xsl:call-template name="Mbean"> 
         <xsl:with-param name="bindings" select="mbean/attribute"/> 
         <xsl:with-param name="bindingsP" select="/book/mbean/attribute/text()"/> 
        </xsl:call-template>  
      </attribute>   
      </mbean> 
     </book> 
    </xsl:template> 
    <xsl:template name="Mbean"> 
     <xsl:param name="bindings"/> 
     <xsl:param name="bindingsP"/>   
      <xsl:choose> 
        <xsl:when test="contains($bindingsP,'@[email protected]')"> 
          <xsl:if test="not(contains(substring-before($bindingsP,'@[email protected]'),'@[email protected]')) and not(contains(substring-before($bindingsP,'@[email protected]'),'@[email protected]')) and not(contains($bindings,concat(substring-before($bindingsP,'@[email protected]'),'@[email protected]')))"> 
           <xsl:copy-of select="concat(substring-before($bindingsP,'@[email protected]'),'@[email protected]')"/> 
           <xsl:call-template name="Mbean"> 
            <xsl:with-param name="bindings" select="$bindings"/> 
            <xsl:with-param name="bindingsP" select="substring-after($bindingsP,'@[email protected]')"/> 
           </xsl:call-template> 
          </xsl:if>       
        </xsl:when> 
        <xsl:when test="contains($bindingsP,'@[email protected]')"> 
          <xsl:if test="not(contains(substring-before($bindingsP,'@[email protected]'),'@[email protected]')) and not(contains(substring-before($bindingsP,'@[email protected]'),'@[email protected]')) and not(contains($bindings,concat(substring-before($bindingsP,'@[email protected]'),'@[email protected]')))"> 
           <xsl:copy-of select="concat(substring-before($bindingsP,'@[email protected]'),'@[email protected]')"/> 
           <xsl:call-template name="Mbean"> 
            <xsl:with-param name="bindings" select="$bindings"/> 
            <xsl:with-param name="bindingsP" select="substring-after($bindingsP,'@[email protected]')"/> 
           </xsl:call-template> 
          </xsl:if>  

        </xsl:when> 
      </xsl:choose>  

    </xsl:template> 

    <xsl:template match="text()"/> 
</xsl:stylesheet> 
+0

不,请参阅我在问题中发布的输出。谢谢你的帮助。 – phani

+0

应该只有一个属性。 – phani

+0

我没有得到预期的输出:'<?xml version =“1.0”encoding =“utf-8”?> <代码的MBean = “org.book.mybooks” 名称= “mycompany.props.jndi:名称= mybookprops”> <属性名= “bookprops”> \t \t \t abc.mybook.onebook = @ NEW_VALUE @ \t \t \t def.mybook.twobook = @ NEW_VALUE @ \t \t \t ghi.myebook.fourbook = @值@ \t \t \t \t \t \t abc.mybook.onebook = @价值@ \t \t \t def.mybook.twobook = @价值@ \t \t \t ghi.myebook.threebook = @价值@ ' – phani