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 June 7th, 2006, 03:45 PM
Registered User
 
Join Date: Jun 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Turning XML into HTML

I have an XML document as follows:

Code:
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="ZoneType_TEXT.xsl"?>
<ZoneContent>
  <paragraph style="Heading2" link="http://www.sqlservercentral.com" linktitle="Sql Server Central" window="1">This is a test</paragraph>
  <paragraph>This is another</paragraph>
  <paragraph>This is another</paragraph>
</ZoneContent>
What I want to do is to itterate through the paragraph tags and convert them into HTML so that Heading 2 wraps everything up in HTML H2 tags.

If There is a link then there will be an <h2><a href="http://www.sqlservercentral.com" title="Sql Server Central" target="1">This is a test paragraph</a></h2>

My XSLT looks as follows

Code:
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:for-each select="ZoneContent/paragraph">
        <xsl:choose>
            <xsl:when test="@style='Heading1'">
                <xsl:element name="h1">
                    <xsl:appy-templates select="."/>
                </xsl:element>
            </xsl:when>
            <xsl:when test="@style='Heading2'">
                <xsl:element name="h2">
                    <xsl:appy-templates select="."/>
                </xsl:element>
            </xsl:when>
            <xsl:when test="@style='Heading3'">
                <xsl:element name="h3">
                    <xsl:appy-templates select="."/>
                </xsl:element>
            </xsl:when>
            <xsl:when test="@style='Heading4'">
                <xsl:element name="h4">
                    <xsl:appy-templates select="."/>
                </xsl:element>
            </xsl:when>
            <xsl:otherwise>
                <xsl:element name="p">
                    <xsl:appy-templates select="."/>
                </xsl:element>
            </xsl:otherwise>
        </xsl:choose>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match=".">
        <xsl:choose>
            <xsl:when test="@link">
            <xsl:element name="a">
                  <xsl:attribute name="href" >
                 <xsl:value-of select="@link" />
               </xsl:attribute>
               <xsl:attribute name="title">
                <xsl:value-of select="@linktitle" />
               </xsl:attribute>
               <xsl:attribute name="target">
                 <xsl:value-of select="@window" />
               </xsl:attribute>
            <xsl:value-of select="current()"/>
            </xsl:element>
            </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="current()"/>
        </xsl:otherwise>
        </xsl:choose>

    </xsl:template>
</xsl:stylesheet>
but it gives errors saying that I can't post at this time.

I am really banging my head against this and should be grateful for any help that can be given.

 
Old June 8th, 2006, 04:36 AM
Registered User
 
Join Date: Jan 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Does this do what you want:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:for-each select="ZoneContent/paragraph">
        <xsl:choose>
            <xsl:when test="@style='Heading1'">
                <xsl:element name="h1">
                </xsl:element>
            </xsl:when>
            <xsl:when test="@style='Heading2'">
                <xsl:element name="h2">
                </xsl:element>
            </xsl:when>
            <xsl:when test="@style='Heading3'">
                <xsl:element name="h3">
                </xsl:element>
            </xsl:when>
            <xsl:when test="@style='Heading4'">
                <xsl:element name="h4">
                </xsl:element>
            </xsl:when>
            <xsl:otherwise>
                <xsl:element name="p">
                </xsl:element>
            </xsl:otherwise>
        </xsl:choose>
        </xsl:for-each>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="paragraph">
        <xsl:choose>
            <xsl:when test="@link">
            <xsl:element name="a">
                  <xsl:attribute name="href" >
                 <xsl:value-of select="@link" />
               </xsl:attribute>
               <xsl:attribute name="title">
                <xsl:value-of select="@linktitle" />
               </xsl:attribute>
               <xsl:attribute name="target">
                 <xsl:value-of select="@window" />
               </xsl:attribute>
            <xsl:value-of select="current()"/>
            </xsl:element>
            </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="current()"/>
        </xsl:otherwise>
        </xsl:choose>

    </xsl:template>
</xsl:stylesheet>


 
Old June 8th, 2006, 05:20 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

match="." is not a valid match pattern. To match all elements, use match="*".

>but it gives errors saying that I can't post at this time.

Please post the actual error message even if you don't understand it. The chances are that someone else does.

Try to solve this problem without using for-each and xsl:choose. It's much better to write a template rule for each kind of element, which does apply-templates to process the children of that element.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
turning off user instances in express JezHarvey BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 6 March 18th, 2008 12:49 PM
Is XML supports transformation of HTML to XML? zeeonline XSLT 1 July 28th, 2006 05:13 PM
Turning Siblings into Children wolfie78uk XSLT 3 December 22nd, 2005 08:33 AM
Turning strings into bits...help patpicos BOOK: Beginning Java 2 2 March 29th, 2005 06:09 AM
turning ' into &#8217; bigmish Classic ASP Basics 4 August 21st, 2004 11:32 AM





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