Page 1 of 1

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

Posted: Thu Jun 08, 2006 11:05 pm
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?

Posted: Fri Jun 09, 2006 8:42 am
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

Posted: Fri Jun 09, 2006 9:04 am
by george
[@name='$TypeVal']
I believe what you want is

[@name=$TypeVal]

Best Regards,
George

Posted: Fri Jun 09, 2006 6:39 pm
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?

Posted: Sat Jun 10, 2006 10:10 am
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