xml转换 - 根据当前节点向前一个节点添加属性
问题描述:
我有一个ejb-jar.xml,标签中没有'id'属性。xml转换 - 根据当前节点向前一个节点添加属性
什么是解决这个问题的最佳方法?可以使用XSLT吗?
<session>
<ejb-name>EJB1</ejb-name>
<local-home>x.E1LH</local-home>
<local>x.E1L</local>
<ejb-class>x.E1EJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
应改写为:
<session id="EJB1"> <!--ejb-name gets added as an "id" attribute -->
<ejb-name>EJB1</ejb-name>
<local-home>x.E1LH</local-home>
<local>x.E1L</local>
<ejb-class>x.E1EJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
答
XSLT肯定能做到这一点...但它确实取决于你需要做什么。
喜欢的东西(未经测试)
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="session">
<session id="{ejb-name}">
<xsl:apply-templates select="@* | node()"/>
</session>
</xsl:template>
+1打我给它。我要写完全一样的东西。 – Tomalak 2009-07-22 08:17:38