我有一个包含以下标记的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>
中,所以我想删除这个父子并创建一个单独的元素{{1}文件。
我在stackoverflow中搜索了几个问题,但看起来它们与我现在遇到的问题并不完全相关。有人可以通过xslt 1.0或2.0来了解如何使用此解决方案。
这是我期待的最终输出:
<mbean>all parent elements and child elements</mbean>
答案 0 :(得分: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>
答案 1 :(得分: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>