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 December 19th, 2005, 10:38 AM
Registered User
 
Join Date: Dec 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Elements that don't match any templates

Hi all,

I'm quite new to XSLT and am having a little bit of trouble. I have a stylesheet that is running through my XML document which contains a list of commands. Once all the known commands have been matched I want to somehow take all the elements that don't match any of my templates and output these as unrecognised commands.
I'm grateful for any suggestions anyone might have,

Thanks in advance,
Clare
 
Old December 19th, 2005, 11:24 AM
Authorized User
 
Join Date: Jul 2004
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Clare, can you give an example of you xml input and expected output? It sounds like you need a catch all template

Meanwhile, here's an example that may help

xml:
----
<commands>
    <command1 />
    <command2 />
    <command3 />
    <command4 />
    <command5 />
</commands>

xslt:
-----
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="commands">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="command1">
           recognised : command1<br />
       </xsl:template>

       <xsl:template match="command2">
           recognised : command2<br />
       </xsl:template>


       <xsl:template match="*">
           unrecognised : <xsl:value-of select="name()" /><br />
       </xsl:template>
</xsl:stylesheet>

output:
-------
recognised : command1
recognised : command2
unrecognised : command3
unrecognised : command4
unrecognised : command5

 
Old December 20th, 2005, 05:58 AM
Registered User
 
Join Date: Dec 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi there,

I will look into the catch all template. Thank you for that.
My XML looks something like this:

<?xml version="1.0" encoding="UTF-8"?>

<PRNFile xmlns="http://www.w3.org/2001/XMLSchema-instance">
    <printerdetails xmlns=""/>
    <commands xmlns="">
        <command position="0">1B5B4B0200001F424A4</command>
        <command position="16">436F6E74726</command>
        <command position="35">53657454</command>
        <command position="58">1B40</command>
     </commands>
 </PRNFile>

I have a number of templates one of which looks something like this:

<xso:template match="command[.='1B40']">
   <name>Return to Neutral Mode</name>
   <code>1B40</code>
</xso:template>

The codes within the command elements represent inkjet printer commands and as I will not be able to "translate" every command I want a template that assuming I have templates to match all the commands in the XML except <command position="35">53657454</command>
then I want to have a template that catches this unknown and outputs say:

<unknowns >
<unknown position="35">53657454</unknown>
</unknowns>

Thanks again,
Clare


 
Old December 21st, 2005, 06:20 AM
Registered User
 
Join Date: Dec 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ah, OK I understand now. The more specific the query is the higher priority the XSLT template is so I can have my various templates seraching for teh values I know about and then just have a "command" template whcih will receive the lowest priority and pick up all the unrecognised elements.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Exclude Elements in Apply Templates mail4kaja XSLT 18 November 29th, 2008 12:09 PM
two different templates for same match attr? gabster XSLT 3 September 6th, 2007 10:56 AM
Changing variables in match templates chicken XSLT 0 September 11th, 2006 10:26 AM
How to Only Match Elements that Equal an XSL Var? tclancy XSLT 1 April 21st, 2006 04:05 PM
how to match elements starting with roopa XSLT 1 October 20th, 2005 10:29 AM





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