Can I 'apply-templates' across multiple XML documents?

Here should go questions about transforming XML with XSLT and FOP.
mphare
Posts: 71
Joined: Fri Apr 30, 2004 8:00 pm
Location: Texas

Can I 'apply-templates' across multiple XML documents?

Post by mphare »

A simple version of what I have it this:

Code: Select all



<!-- First, declare the external XML document -->
<xsl:variable name="generic-common-xsd" select="document('../XSD/Common/NMIE-Generic-Common.xsd')"></xsl:variable>

<!-- Do some processing in the root XML document, then at some point
I need to process something in the other XML document -->
<xsl:element name="Value">
<xsl:apply-templates select="$generic-common-xsd/xs:simpleType[@name='$TypeVal']" mode="Type"></xsl:apply-templates>
</xsl:element>

<!-- Doing this with a template seemed the most logical way, but doesn't seem to work -->
<xsl:template match="xs:simpleType" mode="Type">
<xsl:element name="Domain"></xsl:element>
</xsl:template>
Basically, for ease of re-use, I have separated my schemas across multiple files.

What I have above is not working.
Is this even possible?
--------------------------

- mike

GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876
Radu
Posts: 9420
Joined: Fri Jul 09, 2004 5:18 pm

Post by Radu »

Hi Mike,

As I see it, you have an omission in the xpath used in the "xsl:apply-templates".
It should be like:

Code: Select all

<xsl:apply-templates select="$generic-common-xsd/xs:schema/xs:simpleType[@name='$TypeVal']"
Notice the additional "xs:schema" in the path.

Here is your stylesheet adapted to work with the Oxygen "samples/personal.xsd" file.

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="/">
<!-- First, declare the external XML document -->
<xsl:variable name="generic-common-xsd"
select="document('personal.xsd')"/>

<!-- Do some processing in the root XML document, then at some point
I need to process something in the other XML document -->
<xsl:element name="Value">
<xsl:apply-templates select="$generic-common-xsd/xs:schema/xs:element[@name='personnel']"
mode="elem"/>
</xsl:element>
</xsl:template>
<!-- Doing this with a template seemed the most logical way, but doesn't seem to work -->
<xsl:template match="xs:element" mode="elem">
<xsl:value-of select="@name"/>
</xsl:template>
</xsl:stylesheet>
Regards, Radu
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

[@name='$TypeVal']
I believe what you want is

[@name=$TypeVal]

Best Regards,
George
mphare
Posts: 71
Joined: Fri Apr 30, 2004 8:00 pm
Location: Texas

Post by mphare »

Using a variable for the document() reference doesn't seem to be working.

I replaced it with:

Code: Select all

			
<xsl:element name="Value">
<xsl:apply-templates
select="document('myschema.xsd')/xs:schema/xs:simpleType[@name='SerialNumberType']"
mode="Type"/>
</xsl:element>
And it's a lot happier.

But I've had to hard-code the attribute target ('SerialNumberType') rather than use a variable

I need this to be dynamic as I scoot through the document.

Code: Select all

			<xsl:variable name="TypeVal" select="string(.//xs:element/@type)"/>
and the use it as:

Code: Select all

			
<xsl:variable name="TypeVal" select="string(.//xs:element/@type)"/>
...
<xsl:element name="Value">
<xsl:apply-templates
select="document('myschema.xsd')/xs:schema/xs:simpleType[@name=$TypeVal]"
mode="Type"/>
</xsl:element>
And, yes, you are correct the single quotes were in error.

However, this is not calling the template as I would have expected.

Which brings me to a question about Oxy7.2 and the XSLT debugger.

If I set a break point and stop after the setting of the variables, the debugger does not show me the value in the variable. It has the name of all the vaiables. It shows $TypeVal as a local variable, but the value is empty for all variables and parameters.

Any ideas why?
--------------------------

- mike

GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi Mike,

The debugger shows what the processor has for that variable. In general XSLT processors perform lasy evaluation of variables, they are evaluated not when they are declared but when they are used for the first time.

Best Regards,
George
Post Reply