views:

30

answers:

1

Let's say I have a series of xml files in this format:

A.xml:

<page>
    <header>Page A</header>
    <content>blAh blAh blAh</content>
</page>

B.xml:

<page also-include="A.xml">
    <header>Page B</header>
    <content>Blah Blah Blah</content>
</page>

Using this XSLT:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:template match="/page">
        <h1>
            <xsl:value-of select="header" />
        </h1>
        <p>
            <xsl:value-of select="content" />
        </p>
    </xsl:template>
</xsl:stylesheet>

I can turn A.xml into this:

<h1>
    Page A
</h1>
<p>
    blAh blAh blAh
</p>

But how would I make it also turn B.xml into this?

<h1>
    Page B
</h1>
<p>
    Blah Blah Blah
</p>
<p>
    blAh blAh blAh
</p>

I know that I need to use document(concat(@also-include,'.xml')) somewhere, but I'm not sure where.


Oh, and the catch is, I need this to still work if B were to be included in a third file, C.xml.

Any idea as to how to do this?

+2  A: 

It is possible:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="page">
    <h1>
      <xsl:value-of select="header"/>
    </h1>
    <p>
      <xsl:apply-templates select="." mode="content"/>
    </p>
  </xsl:template>

  <xsl:template match="page" mode="content">
    <xsl:value-of select="content"/>
    <xsl:if test="@include">
      <xsl:apply-templates select="document(@include)" mode="content"/>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>
Jan Willem B
I thought the `mode` attribute might be what I wanted. Let me test that in my scenario
Eric
Thanks, that pushed me in the right direction
Eric
Ok, that push wasn't quite large enough. My example was overly simplified. Could you perhaps have a look at [this](http://stackoverflow.com/questions/2907863/recursive-xslt-part-2) harder problem?
Eric