Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 9th, 2009, 09:52 PM
Registered User
 
Join Date: Jul 2009
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default Another question about generating XSLT with XSLT

I understand how to use xsl:namespace-alias to generate XSLT documents as the result of XSLT transforms.

However, I'm still having trouble writing a stylesheet that will output a second stylesheet containing namespace-aware strings. An example will help:

Suppose I have an input document,

<stylesheet-descriptor>
<namespace>http://www.example.com</namespace>
<element-name>foo</element-name>
</stylesheet-descriptor>

How do I build a stylesheet that will take this input document and output an XSLT stylesheet that contains portions like:

<xsl:template match="e:foo" xmlns:e="http://www.example.com">

or like

<output-element output-attribute="{e:foo}" xmlns:e=
"http://www.example.com" />

?

Thanks very much,
Dan
 
Old July 16th, 2009, 04:16 PM
Authorized User
 
Join Date: Apr 2008
Posts: 70
Thanks: 17
Thanked 1 Time in 1 Post
Send a message via Yahoo to iceandrews
Default

This is quick and dirty and definiately not the best possible solution, but it may lead down the right path. You should note that this solution only works for XSLT 2.0. It's written for the exact input document you've provided.

Code:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
    <xsl:template match="/">
        <xsl:apply-templates select="stylesheet-descriptor"/>
    </xsl:template>
 
    <xsl:template match="stylesheet-descriptor">
        <xsl:variable name="Prefix" select="'e'" />
        <xsl:variable name="Namespace" select="string(namespace)" /> 
        <xsl:element name="xsl:template" namespace="http://www.w3.org/1999/XSL/Transform">
            <xsl:attribute name="match" select="concat($Prefix,':',element-name)"/>
            <xsl:namespace name="{$Prefix}" select="$Namespace" />
        </xsl:element>
    </xsl:template>
 
</xsl:stylesheet>
The Following User Says Thank You to iceandrews For This Useful Post:
danblick (July 16th, 2009)
 
Old July 16th, 2009, 08:40 PM
Registered User
 
Join Date: Jul 2009
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default another solution

Thanks iceandrews. After looking into this a bit more, I concluded:

1) it's not possible to do what I originally wanted with XSLT versions < 2.0.

2) With XSLT, the xsl:namespace element works well.

Supposing that stylesheet-descriptor is in the xmlns "http://www.example.com/ns/1"; that is, something like:

<?xml version="1.0"?>
<stylesheet-descriptor xmlns="http://www.example.com/ns/1">
<namespace>http://www.example.com</namespace>
<element-name>foo</element-name>
</stylesheet-descriptor>


This stylesheet (note version 2.0):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:example="http://www.example.com/ns/1" xmlns:axsl="http://www.w3.org/1999/XSL/TransformAlias" version="2.0" exclude-result-prefixes="example">
<xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="example:stylesheet-descriptor"/>
</xsl:template>
<xsl:template match="example:stylesheet-descriptor">
<axsl:stylesheet version="1.0">
<axsl:output method="xml" doctype-public="-//W3C//DTD XHTML 1.1//EN" doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" indent="yes"/>
<axsl:template match="/">
<!-- do something here -->
<foo xmlns="http://www.example.com/other-ns/1">
<axsl:apply-templates select="input:{example:element-name}">
<xsl:namespace name="input" select="example:namespace"/>
</axsl:apply-templates>
</foo>
</axsl:template>
<axsl:template match="input:{example:element-name}">
<xsl:namespace name="input" select="example:namespace"/>
<bar xmlns="http://www.example.com/other-ns/1"/>
<!-- do something else here -->
</axsl:template>
</axsl:stylesheet>
</xsl:template>
</xsl:stylesheet>

produces the right output:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" doctype-public="-//W3C//DTD XHTML 1.1//EN"
doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
indent="yes"/>
<xsl:template match="/">
<foo xmlns="http://www.example.com/other-ns/1">
<xsl:apply-templates xmlns:input="http://www.example.com" select="input:foo"/>
</foo>
</xsl:template>
<xsl:template xmlns:input="http://www.example.com" match="input:foo">
<bar xmlns="http://www.example.com/other-ns/1"/>
</xsl:template>
</xsl:stylesheet>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Generating XSLT with XSLT stonis XSLT 3 April 1st, 2008 08:17 PM
Generating a comma-delimited data in XSLT dude153 XSLT 2 November 30th, 2007 12:12 PM
Dynamically generating Javascript (XSLT in IE6) einszwei XSLT 1 June 28th, 2007 01:07 PM
generating a table w. xslt, newbie problem themuffinman_swe XSLT 5 November 26th, 2005 01:05 PM
Generating aspx pages with xml and xslt glaseur XSLT 4 December 3rd, 2003 05:27 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.