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 February 23rd, 2009, 07:50 PM
Registered User
 
Join Date: Feb 2009
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default Help transforming mixed complexType

Hello all,

I am quite new with XML and XSLT, have been making good progress with tutorials and forums. I am transforming a XML file into HTML but I cannot figure out how to transform the pieces that make up my mixed complexType. I have searched and searched and come up with nothing. I am sure it is blindingly obvious but i just can not get it.

Here is my xsd:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="service_catalogue">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="catalogue_name" type="xs:string"/>
                <xs:element name="catalogue_description" type="content"/>
                <xs:element name="service_area" minOccurs="1" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="area_name" type="xs:string"/>
                            <xs:element name="area_description" type="content"/>
                            <xs:element name="service_category" minOccurs="1" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="category_name" type="xs:string"/>
                                        <xs:element name="category_description" type="content"/>
                                        <xs:element name="service" minOccurs="1" maxOccurs="unbounded">
                                            <xs:complexType>
                                                <xs:sequence>
                                                    <xs:element name="service_name" type="xs:string"/>
                                                    <xs:element name="service_description" type="content"/>
                                                    <xs:element name="availability" type="content"/>
                                                    <xs:element name="service_target" type="content"/>
                                                    <xs:element name="service_constraints" type="content"/>
                                                    <xs:element name="customer_responsibilities" type="content"/>
                                                    <xs:element name="service_access" type="content"/>
                                                    <xs:element name="service_owner" type="xs:string"/>
                                                    <xs:element name="service_client" type="xs:string"/>
                                                    <xs:element name="service_output" type="xs:string"/>
                                                    <xs:element name="service_type" type="xs:string"/>
                                                    <xs:element name="service_int_ext" type="xs:string" />
                                                </xs:sequence>
                                            </xs:complexType>
                                        </xs:element>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    
    <xs:complexType name="content">
        <xs:choice maxOccurs="unbounded" minOccurs="0">
            <xs:element name="para" type="para_content"/>            
       </xs:choice>
    </xs:complexType>
    
    <xs:complexType name="para_content" mixed="true">
        <xs:choice maxOccurs="unbounded" minOccurs="0">
            <xs:element name="unordered_list" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="ulist_item" type="xs:string" maxOccurs="unbounded"></xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="glossary_link" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
            
        </xs:choice>
    </xs:complexType>
    
</xs:schema>
Here is a relevant chunk of my xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="AG_Layout.xsl"?>
<service_catalogue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="AG_ServiceCat_Content.xsd">

    
    <catalogue_name>ITSD - Service Catalogue</catalogue_name>
    <catalogue_description>
        <para>The ITSD Service Catalogue has been developed to define customer services. The service definitions focus on providing customers with a clear statement of what is available, what customers need to do to get the service, and ITSD’s service target.</para>
        <para>On the left portion of the screen, you will find links to ITSD services, which have been organized under general service categories. On the right, there are links to supporting materials and quick access to ITSD Service Desk.</para>
    </catalogue_description>
    
    <service_area>
        <area_name>Core Services</area_name>
        <area_description><para>Core Services Description goes here</para></area_description>
        
        <service_category>
            <category_name>Service Requests</category_name>
            <category_description><para>This service facilitates requests and tracks the approval process for services requested by ITSD customers.</para></category_description>
            
            <service>
                <service_name>Set Up New Employee</service_name>
                <service_description><para>Sets up all IT related services for new employees including access to the government network, government standard software and hardware, Ministry supported applications and other IT related services.</para></service_description>
                <availability><para>This service is available to ITSD customers during <glossary_link>Standard Business Hours</glossary_link></para></availability>
                <service_target><para>To Be Determined</para></service_target>
                <service_constraints><para><glossary_link>Standard Service Constraints</glossary_link></para></service_constraints>
                <customer_responsibilities><para><glossary_link>Standard Customer Responsibilities</glossary_link> Customers will be required to fill in all required fields in the "New Hire Form". Customers are expected to provide additional information (if required) to ITSD in a timely fashion.</para></customer_responsibilities>
                <service_access><para>To obtain this service simply click on the Service Request button below, or, contact your Service Manager at 250 555-1212</para></service_access>
                <service_owner/>
                <service_client></service_client>
                <service_output></service_output>
                <service_type>Transactional</service_type>
                <service_int_ext>Both</service_int_ext>
            </service>
</service_category>
    </service_area>
</service_catalogue>
And finaly here is the relevant chunk of my xsl:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output method="html"/>
    
   <xsl:template match="/">
        <html>
            <head>
                <META http-equiv="Expires" content="0"/>
                <meta http-equiv="Cache-Control" content="no-cache"/> 
                <meta http-equiv="Pragma" content="no-cache"/> 
                <script type="text/javascript" language="JavaScript" src="general.js"></script>
                <link rel="stylesheet" type="text/css" href="style-gov.css"></link>
                    <link rel="stylesheet" type="text/css" href="stylesheets/stylenn4.css"></link>
                
            </head>
            <body >
                <h2><xsl:value-of select="service_catalogue/catalogue_name"/></h2>
                <p><xsl:value-of select="service_catalogue/catalogue_description"/></p>
                
                <xsl:for-each select="service_catalogue/service_area">
                    <table>
                        <tr>
                            <td><b>Service Area:</b></td>
                            <td><xsl:value-of select="area_name"/></td>
                        </tr>
                        <tr>
                            <td><b>Service Area Description:</b></td>
                            <td><xsl:value-of select="area_description"/></td>
                        </tr>                        
                    </table>
                    <br/>
                    <table border="1">
                        <xsl:for-each select="service_category">
                            <xsl:variable name="serv_cat_name" select="category_name"/>
                            <tr bgcolor="#9acd32">
                                <th align="left">Service Category</th>
                                <th align="left"><a name="{$serv_cat_name}"></a>Service Category Description</th>
                            </tr>
                            <tr>
                                <td><b><xsl:value-of select="category_name"/></b></td>
                                <td>
                                    
                                <xsl:value-of select="category_description"/>
                               
                                    </td>
                            </tr>
</table>
                     </xsl:for-each>
                
            </body>
        </html>
    </xsl:template>
In the xsl
Code:
<xsl:value-of select="category_description"/>
is the spot where I need to transform the complexType. What I want to do is to turn all of the
Code:
<glossary_link>
into
Code:
<a href="">
Any help here would be greatly appreciated.

Dave
 
Old February 23rd, 2009, 08:01 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It looks as if you haven't yet learned how to use xsl:apply-templates and template rules. These lie at the heart of the XSLT processing model, and code written without them is really going against the grain of the language. In particular, processing mixed content without using template rules is a nightmare; with them it is a dream.

I haven't looked at your logic in detail, but I would think the first thing you need to do is to replace the instruction <xsl:value-of select="category_description"/> with a call on xsl:apply-templates, and then write a set of template rules for each of the different child elements that might be encountered.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
davegreig1978 (February 25th, 2009)
 
Old February 25th, 2009, 02:46 PM
Registered User
 
Join Date: Feb 2009
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thank you very much, I know it seems simple but this was exactly the little nudge in the right direction that I needed. I had looked at templates and template matching but I didn't understand that the text part of the mixed content would still be output even though there were template matches to the contained elements. For anyone that is interested here is the solution that I have come up with:

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="para">
        <p>
            <xsl:apply-templates/>
        </p>
    </xsl:template>

    <xsl:template match="glossary_link">
        <a href="#"><xsl:value-of select="."/></a>
    </xsl:template>
    
    <xsl:template match="unordered_list">
        <ul>
            <xsl:apply-templates/>
        </ul>
    </xsl:template>
    
    <xsl:template match="ulist_item">
        <li><xsl:value-of select="."/></li>
    </xsl:template>

    <xsl:template match="/">
        <html>
            <head>
                <script type="text/javascript" language="JavaScript" src="general.js"/>
                <link rel="stylesheet" type="text/css" href="style-gov.css"/>
                <link rel="stylesheet" type="text/css" href="stylesheets/stylenn4.css"/>

            </head>
            <body>
                <h2><xsl:value-of select="service_catalogue/catalogue_name"/></h2>
                
<!-- catalogue_description has a complex content type -->
                <p><xsl:apply-templates select="service_catalogue/catalogue_description"/></p>

.....





Similar Threads
Thread Thread Starter Forum Replies Last Post
XML Schema complexType union rushman XML 10 February 28th, 2008 06:03 PM
mixed content parsing joeri XSLT 2 October 7th, 2007 04:54 PM
Copy element into complexType in XSLT 2BOrNot2B XSLT 3 December 20th, 2006 09:09 AM
ComplexType Import Flat File Problem pn609 Biztalk 0 January 20th, 2005 10:10 AM
mixed mode authentication Tremmorkeep SQL Server 2000 6 June 21st, 2004 12:14 PM





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