LISTSERV mailing list manager LISTSERV 16.0

Help for XML-L Archives


XML-L Archives

XML-L Archives


XML-L@LISTSERV.HEANET.IE


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

XML-L Home

XML-L Home

XML-L  April 2001

XML-L April 2001

Subject:

Re: problem is displaying the xml in HTML form

From:

"John E. Simpson" <[log in to unmask]>

Reply-To:

[log in to unmask]

Date:

Tue, 10 Apr 2001 09:42:39 -0400

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (111 lines)

As Janning said, what's happening is that an XSLT stylesheet (actually,
the XSLT processor) comes with built-in template rules which apply if
there's no explicit template rule for a given node. Then one solution,
if this creates problems, is to override these built-ins, as he sort of
showed here:

Janning Vygen wrote:

> <xsl:template match="/">
>   <html>
>   <head>
>   </head>
>   <body>
>     <xsl:apply-templates select="Contents"/>
>   </body>
>   </html>
> </xsl:template>

However, you'll find that this also doesn't quite work. The
xsl:apply-templates element says to look for and apply any template
rules in effect for *children* of the current node. By using the select
attribute, you're saying to do so for only the specified child(ren), not
*all* children, of the current node.

In the above, the current node -- set by the match="/" -- is the source
tree's root node. That has exactly one child, the root ELEMENT named
"Book." The select attribute says to process only the child(ren) named
"Contents"; since the current node has no such children, the effect is
to suppress the output explicitly associated with ANY children of the
root node.

The simplest way to fix this is to replace the "/" match pattern with
"Book" -- because that element does indeed have a child named
"Contents." And by using an explicit select="Contents" attribute,
Janning has effectively suppressed the contents of the Title element,
which is one effect you sought.

But then the XSLT processor hits this in Janning's solution:

> <xsl:template match="Contents">
>   <xsl:for-each select="Chapters">
>      <xsl:apply-templates select = "Heading"/><hr></hr>
>   </xsl:for-each>
> </xsl:template>

This template rule is not fired as a result of the earlier
xsl:apply-templates (for children of the root node), because (as I
explained) there are NO children of the root node named "Contents."
Rather, it's fired for the simplest of reasons -- because its match
pattern corresponds to the name of an element type in the source tree.
There's exactly one such element in your sample XML, so this template
rule will fire exactly once.

Unfortunately this template rule is just a little bit scrambled,
although it's headed in the right direction. The problem is the
xsl:apply-templates WITHIN the xsl:for-each. You don't need that. Just
do this instead:

    <xsl:template match="Contents">
       <xsl:for-each select="Chapters">
          <xsl:value-of select="Heading"/><hr></hr>
       </xsl:for-each>
    </xsl:template>

The xsl:value-of element transfers text content from the source to the
result tree. The XPath expression used as the value of the select
attribute is relative to the context node, which (here) is the Chapters
element currently being processed by the xsl:for-each. That is, the
xsl:value-of gets the Heading CHILD of the current Chapters element, and
transfers it to the result tree. The rest of the template consists of
just a horizontal rule.

You'll find that there are still some problems with the stylesheet,
aside from the above:

(1) The correct namespace declaration for elements in the XSLT namespace is:
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

(2) Your xsl:stylesheet element also needs to include a version="1.0"
attribute/value.

(3) You probably don't need the xsl:output element here, but if you want
to use it I suggest making the value of its method attribute value all
lowercase, e.g. method="html". (Some processors may forgive the
uppercase, even though it's not a legitimate value.)

After fixing all this up and running the code through the Saxon XSLT
processor, you get a result tree which looks like this (I added a title
element to the head, which helps confirm that the template rule for the
root Book element is firing correctly):

    <html>
       <head>
          <title>Book Headings</title>
       </head>
       <body>
          intro to Java
          <hr>Creating First Java Program
          <hr>Writing thread based applications
          <hr>Socket Programming in JAVA
          <hr>
       </body>
    </html>

Which I think is what you were after.


================================================================
John E. Simpson          | "When I was a little kid we had a sand
http://www.flixml.org    | box. It was a quicksand box. I was an
XML Q&A: www.xml.com     | only child... eventually." (Steven Wright)

Top of Message | Previous Page | Permalink

Advanced Options


Options

Log In

Log In

Get Password

Get Password


Search Archives

Search Archives


Subscribe or Unsubscribe

Subscribe or Unsubscribe


Archives

February 2017
August 2016
June 2016
March 2016
January 2016
July 2014
April 2014
January 2014
July 2013
February 2013
September 2012
August 2012
October 2011
August 2011
June 2011
January 2011
November 2010
October 2010
July 2010
June 2010
March 2010
February 2010
January 2010
November 2009
September 2009
August 2009
July 2009
May 2009
March 2009
December 2008
October 2008
August 2008
May 2008
March 2008
February 2008
January 2008
December 2007
October 2007
August 2007
June 2007
March 2007
January 2007
December 2006
September 2006
July 2006
June 2006
April 2006
February 2006
January 2006
November 2005
September 2005
August 2005
July 2005
June 2005
May 2005
March 2005
January 2005
October 2004
August 2004
July 2004
June 2004
May 2004
March 2004
February 2004
January 2004
December 2003
November 2003
October 2003
September 2003
August 2003
July 2003
June 2003
May 2003
April 2003
March 2003
February 2003
January 2003
December 2002
November 2002
October 2002
September 2002
August 2002
July 2002
June 2002
May 2002
April 2002
March 2002
February 2002
January 2002
December 2001
November 2001
October 2001
September 2001
August 2001
July 2001
June 2001
May 2001
April 2001
March 2001
February 2001
January 2001
December 2000
November 2000
October 2000
September 2000
August 2000
July 2000
June 2000
May 2000
April 2000
March 2000
February 2000
January 2000
December 1999
November 1999
October 1999
September 1999
August 1999
July 1999
June 1999
May 1999
April 1999
March 1999
February 1999
January 1999
December 1998
November 1998
October 1998
September 1998
August 1998
July 1998
June 1998
May 1998
April 1998
March 1998
February 1998
December 1997
November 1997
October 1997

ATOM RSS1 RSS2



LISTSERV.HEANET.IE

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager