I have a source document:
<?xml version="1.0"?>
<source>
<ItemNotSubstituted/>
<ItemToBeSubstituted Id='MatchId' />
</source>
And a stylesheet containing the content that I want to replace in the source:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml" omit-xml-declaration="no" version="1.0"/>
<xsl:preserve-space elements="//*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ItemToBeSubstituted[@Id = 'MatchId']">
<xsl:copy>
<xsl:copy-of select="@*|*"/>
<Element1/>
<Element2 Value="foo"/>
<Element3 Value="bar"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
This stylesheet succesfuly copies <Element1/><Element2 Value="foo"/><Element3 Value="bar"/>to ItemToBeSubstituted. But when I use another source document that ItemToBeSubstitutedalready has content:
<?xml version="1.0"?>
<source>
<ItemNotSubstituted/>
<ItemToBeSubstituted Id='MatchId'>
<Element3 Value="baz"/>
</ItemToBeSubstituted>
</source>
I get this output:
<?xml version="1.0"?>
<source>
<ItemNotSubstituted/>
<ItemToBeSubstituted Id="MatchId">
<Element3 Value="baz"/>
<Element1/>
<Element2 Value="foo"/>
<Element3 Value="bar"/>
</ItemToBeSubstituted>
</source>
I would only like to replace elements from the stylesheet that do not yet exist in the source document. This is the result I'm looking for after applying the stylesheet to the second document, only with an element <Element3>from the original document:
<?xml version="1.0"?>
<source>
<ItemNotSubstituted/>
<ItemToBeSubstituted Id="MatchId">
<Element3 Value="baz"/>
<Element1/>
<Element2 Value="foo"/>
</ItemToBeSubstituted>
</source>
XSL? , . , <xsl:if> . , , , ?