tags:

views:

109

answers:

1

I've got an ejb-jar.xml that doesn't have an 'id' attribute in the tag.

What would be the best way to fix this? Could XSLT be used for this?

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

Should be rewritten as:

<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>
+1  A: 

xslt can certainly do it... but it really depends on what you need to do.

Something like (untested)

<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>
Marc Gravell
+1 Beat me to it. I was going to write the exact same thing.
Tomalak
I'd rather use `xsl:copy` and `xsl:attribute` there (just in case that `<session>` has any other attributes).
Pavel Minaev
Any other attributes will still be copied by the apply-templates @*
Marc Gravell