2. scan xml source elements according to the order in the document text
function doElement(xmlElement) {
//try if we have a template that can apply this element
templeate1=all the best matched template from all templates;
if (template1 is not null) {
apply template1 to xmlElement;
return;
}
//go deeper to the child elements
children=get all children of xmlement;
for each element1 of children {
doElement(element1)
}
}
<?xml version="1.0" encoding="UTF-8"?>
<root>
<AA id="aa1"> AA1 </AA>
<AA id="aa2"> Have BB
<BB id="aa2bb1"> AA2BB1 </BB>
</AA>
<CC id="cc1">
<BB id="cc1bb2"></BB>
</CC>
<BB id="bb1"> BB1 </BB>
<BB id="bb2"> BB2 </BB>
<BB id="bb3"> BB3 </BB>
<AA id="aa4"> AA4 </AA>
<AA id="aa5"> AA5 </AA>
</root>
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="BB">
<b>
name=<xsl:value-of select="name()"/>
id=<xsl:value-of select="@id"/>
</b>
</xsl:template>
<xsl:template match="AA">
<a>
<xsl:value-of select="name()"/>
<xsl:value-of select="@id"/>
</a>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<a>AAaa1</a>
<a>AAaa2</a>
<b>
name=BB
id=cc1bb2</b>
<b>
name=BB
id=bb1</b>
<b>
name=BB
id=bb2</b>
<b>
name=BB
id=bb3</b>
<a>AAaa4</a>
<a>AAaa5</a>