Yesterday I asked a question about loading external XMLs dynamically from XSLTs. It was answered, but now I have another problem. If you read that question you'll see an <include ref="/path/to/some.xml" />
at the third level. What if the level at which the <include>
occurs varies?
Is there a way to dynamically choose to extract the nodes from that XML on the same level on which they occur in the original file?
EDIT: To be clear: I'm talking about the "element path" in the XML (the position of the include element in the XML), not the file path.
merge.xslt:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output indent="yes" method="xml" encoding="utf-8"
omit-xml-declaration="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="include">
<!-- the depth of the extraction path (fruit/*/*/*... etc) should
be based on the position of the matched <include> element -->
<xsl:apply-templates select="document(@ref)/fruit/*/*/*"/>
</xsl:template>
</xsl:stylesheet>
fruit.xml:
<fruit>
<local>
<apples>
<include ref="apples.xml" />
</apples>
</local>
<exotic>
<bananas>
<deeperlevel>
<include ref="bananas.xml" />
</deeperlevel>
</bananas>
<oranges>
<include ref="oranges.xml" />
</oranges>
</exotic>
</fruit>
bananans.xml:
<fruit>
<exotic>
<bananas>
<deeperlevel>
<banana type="chiquita" color="yellow" />
<banana type="generic" color="yellow" />
</deeperlevel>
</bananas>
</exotic>
</fruit>
result:
<fruit>
<local>
<apples>
<apple type="jonagold" color="red"/><apple type="granny-smith" color="green"/>
</apples>
</local>
<exotic>
<bananas>
<deeperlevel>
<deeperlevel> <!-- I don't want the second <deeperlevel> here, instead
<bananas .../> should be extracted, right after the
first <deeperlevel> -->
<banana type="chiquita" color="yellow"/>
<banana type="generic" color="yellow"/>
</deeperlevel>
</deeperlevel>
</bananas>
<oranges>
<orange type="juice-orange" color="orange"/><orange type="red-orange" color="red"/>
</oranges>
</exotic>
</fruit>