0
votes

I am creating XSLT styelsheet to extract weather forecast data of different cities from XML documents. Currently, I have 3 XML files. But I am not getting idea to process multiple XML documents from one XSLT stylesheet. I want to use my one city's XML file as the main XML document of my application and call that one XSLT file for all multiple XML documents.

This is my XSLT page for melbourne:

<xsl:template match="/">
        <xsl:text>Melbourne</xsl:text><br/>
        <xsl:for-each select="product/forecast/area[@description='Melbourne']/forecast-period">
            <xsl:value-of select="text[@type='forecast']"/>
            <xsl:value-of select="text[@type='precis']"/>
            <xsl:value-of select="element[@type='air_temperature_minimum']"/>,
            <xsl:value-of select="element[@type='air_temperature_maximum']"/><br/>
        </xsl:for-each>
        </xsl:template>

I am calling this xsl page from melbourne.xml. And I want to use melbourne.xml as a main document and process other cities from here.

Can you guys give me some idea?

P.S. I am new to XML and XSLT.

1

1 Answers

0
votes

You can use the XSLT function document() to open an external XML document. It returns the document(s) node. It takes as argument the document URI(s). An optional 2nd argument tells how to interpret relative URIs.

Perhaps a better option is to define as input to your XSLT a meta-document, which contains the URI of the documents you want to process:

<cities>
  <city url="melbourne.xml"/>
  <city url="timbuktu.xml"/>
</cities>

Then your XSLT can start with something like:

<xsl:template match="/cities">
  <xsl:apply-templates select="document(city/@url, .)/product"/>
</xsl:template>

Note: you'll nee to modify your template to match "product" instead of /, and change the for-each accordingly.