Can you embed XML in XSL and access it?

J

johkar

I would like to embed some XML within my XSL as either a param or a
global variable. Is this possible? If so, what is the syntax to
access it?

<xsl:call-template name="securetemplate">
<xsl:with-param name="leftnav">
<links>
<link>Link 1</link>
<link>Link 2</link>
</links>
</xsl:with-param>
</xsl:call-template>

Thanks, John
 
A

Andy Dingley

johkar said:
I would like to embed some XML within my XSL as either a param or a
global variable. Is this possible?


Yes. Stick it into your XSL as XML elements within a separate
namespace (and an easily findable root element). The use the XPath
document() function to access it.
 
M

Martin Honnen

johkar said:
I would like to embed some XML within my XSL as either a param or a
global variable. Is this possible? If so, what is the syntax to
access it?

You can embed XML data as a top level element (child of the
xsl:stylesheet element) if the element is in a different namespace e.g.

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:data="http://example.com/2006/some-data"
version="1.0">

<data:data>
<links xmlns="">
<link>Link 1</link>
<link>Link 2</link>
</links>
</data:data>

The XSLT stylesheet itself can be accessed with calling the document
function with '' e.g.
document('')
so you would get at that data with e.g.
document('')/xsl:stylesheet/data:data/links/link
to access all the link elements.


If you use xsl:param or xsl:variable and have contents in these elements
then you have a result tree fragment as the parameter or variable value
that you can later copy to the result tree with xsl:copy-of e.g.

<xsl:call-template name="securetemplate">
<xsl:with-param name="leftnav">
<links>
<link>Link 1</link>
<link>Link 2</link>
</links>
</xsl:with-param>
</xsl:call-template>

<xsl:template name="securetemplate">
<xsl:param name="leftnav"/>
<xsl:copy-of select="$leftnav"/>
</xsl:template>
 
J

johkar

johkar said:
I would like to embed some XML within my XSL as either a param or a
global variable. Is this possible? If so, what is the syntax to
access it?

Excellent. Thank you both. That was record response time to boot.

John
 
J

johkar

Martin said:
You can embed XML data as a top level element (child of the
xsl:stylesheet element) if the element is in a different namespace e.g.

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:data="http://example.com/2006/some-data"
version="1.0">

<data:data>
<links xmlns="">
<link>Link 1</link>
<link>Link 2</link>
</links>
</data:data>

The XSLT stylesheet itself can be accessed with calling the document
function with '' e.g.
document('')
so you would get at that data with e.g.
document('')/xsl:stylesheet/data:data/links/link
to access all the link elements.


If you use xsl:param or xsl:variable and have contents in these elements
then you have a result tree fragment as the parameter or variable value
that you can later copy to the result tree with xsl:copy-of e.g.

<xsl:call-template name="securetemplate">
<xsl:with-param name="leftnav">
<links>
<link>Link 1</link>
<link>Link 2</link>
</links>
</xsl:with-param>
</xsl:call-template>

<xsl:template name="securetemplate">
<xsl:param name="leftnav"/>
<xsl:copy-of select="$leftnav"/>
</xsl:template>

One more question. If data:data is on the main page, how can you
access it in an included xsl?

John
 
J

Joe Kesselman

johkar said:
One more question. If data:data is on the main page, how can you
access it in an included xsl?

I'm not sure what you mean by "main page" in this context. The main
stylesheet? If you know the URI for that stylesheet, you can ask the
document function to retrieve from that specific URI. If you don't know
the URI, you may have to pass it down as a parameter ... or just pass a
reference to that data:data element down as a parameter.
 
J

johkar

Joe said:
I'm not sure what you mean by "main page" in this context. The main
stylesheet? If you know the URI for that stylesheet, you can ask the
document function to retrieve from that specific URI. If you don't know
the URI, you may have to pass it down as a parameter ... or just pass a
reference to that data:data element down as a parameter.

I have only been able to get one scenario to work, if I have the
data:data namespace and the document function on the same stylesheet.
If I want to output various nodes from the data:data XML from and
included stylesheet, I cannot get it to work. I have tried passing
references and other things suggested, but my syntax is apparently
incorrect. We use Xalan if that makes a difference. Below is an
example.

Thanks, John

Main template
<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:data="http://example.com/2006/some-data">
<xsl:include href="common/leftcol.xsl" />

<data:data>
<links xmlns="">
<link name="selected">Link 1</link>
<link>Link 2</link>
</links>
</data:data>

<xsl:template match="/">
<xsl:call-template name="leftcol" />
</xsl:template>
</xsl:stylesheet>

Left Column Template

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:data="http://example.com/2006/some-data">
<xsl:template name="leftcol">
<xsl:value-of
select="document(common/mainPage.xsl)/data:data/links/link[@name='selected']"
/>
</xsl:template>
</xsl:stylesheet>
 
M

Martin Honnen

johkar wrote:

<xsl:value-of
select="document(common/mainPage.xsl)/data:data/links/link[@name='selected']"
/>

I think you want/need
document('common/mainPage.xsl')/xsl:stylesheet/data:data/links/link[@name='selected']
 
J

johkar

Martin said:
I think you want/need
document('common/mainPage.xsl')/xsl:stylesheet/data:data/links/link[@name='selected']

Sorry, this is my directory structure:

common/leftcol.xsl
main/mainPage.xsl (this is the main stylesheet)

I tried both of these in leftcol.xsl, but nothing has worked so far:

document('main/mainPage.xsl')/xsl:stylesheet/data:data/links/link[@name='selected']

and

document('../main/mainPage.xsl')/xsl:stylesheet/data:data/links/link[@name='selected']
 
J

johkar

Martin said:
johkar wrote:

<xsl:value-of
select="document(common/mainPage.xsl)/data:data/links/link[@name='selected']"
/>

I think you want/need
document('common/mainPage.xsl')/xsl:stylesheet/data:data/links/link[@name='selected']

Ok, I don't know what happened, but since I couldn't get the path
correct, I tried passing a reference to the document() and it worked
this time...go figure.

So within my call-template, I included a reference. I know I tried
this before.
<xsl:with-param name="leftcol" select="document('')" />

Thanks everyone.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,006
Messages
2,570,265
Members
46,860
Latest member
JeremiahCo

Latest Threads

Top