I need to add elements to an element, creating it first if it doesn't already exist.

My desired final result, after adding ABC and DEF, is:

```
<?xml version="1.0" encoding="utf-8"?>
<A xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Q/>
  <B>
    <string>ABC</string>
    <string>DEF</string>
  </B>
<A>

```

I thought that the following would accomplish this:

```
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- Identity transform -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <!-- Insert a B element if it doesn't exist. -->
  <xsl:template match="A[not(B)]">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
        <B/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="B">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
      <string>ABC</string>
      <string>DEF</string>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

```

If I start with the following, in which <B> already exists, it works fine, returning the result above:

```
<?xml version="1.0" encoding="utf-8"?>
<A xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org
  <Q/>
  <B/>
</A>

```

However, if I don't have a <B>, as in below:

```
<?xml version="1.0" encoding="utf-8"?>
<A xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org
  <Q/>
</A>

```

then it creates the <B> as below, but doesn't insert ABC and DEF:

```
<?xml version="1.0" encoding="utf-8"?>
<A xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org
  <Q/>
  <B/>
</A>

```

So, what am I missing? Thanks in advance.