xsl-list
[Top] [All Lists]

Re: Mixed Contents

2004-02-12 13:46:32
Sandeep Deshpande wrote:

Hi All,
I have a problem with the elements having mixed content. The problem is as
follows.

======= XML File (x.xml ) ================
<?xml-stylesheet type="text/xsl" href="x.xsl"?>
<directory>Hi I am in a directory.
<subdirectory>Hi I am in a subdirectory.
<file>Hi I am in file1.</file>
in between two files (back to subdirectory.)
<file>Hi I am in file2.</file>
in between two files (back to subdirectory.)
<file>Hi I am in file3.</file>
out of file. back to subdirectory.</subdirectory>
out of subdirectory. back to directory.</directory>
================================

======= XSL File (x.xsl ) ================
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/TR/WD-xsl";>
                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Fix this first, according to the hint in the other post.
Actually, the rest of the code you provided is standard
XSLT rather that WD-XSL.
...
<xsl:template match="directory">
 <br/><xsl:value-of select="text()"/><br/>
<xsl:apply-templates select="subdirectory"/>
 <br/><xsl:value-of select="text()[1]"/><br/>
</xsl:template>

That's convoluten and wont do what you expect. All the
XPath expressions are relative to the context node, which
means that the first as well as the second xsl:value-of
result in the same text, specifically "Hi I am in a directory..."
(trailing whitespace replaced by ellipsis).
You have to think in terms of traversing a tree rather
than pulling text snippet from a string.

The following will be closer to your requirements:

 <xsl:stylesheet version="1.0"
   "xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
   <xsl:template match="/">
     <html><head><title/</head>><body>
       <xsl:apply-templates/>
     </body></html>
   </xsl:template>
   <xsl:template match="directory">
     <xsl:apply-templates/>
     <br/>
   </xsl:template>
   <xsl:template match="subdirectory">
     <font color="red">
       <xsl:apply-templates/>
     </font><br/>
   </xsl:template>
   <xsl:template match="file">
     <font color="blue">
       <xsl:apply-templates/>
     </font><br/>
   </xsl:template>
 </xsl:stylesheet>

This will omit some line breaks. Adding the following template
   <xsl:template match="text()">
     <xsl:apply-templates/><br/>
   </xsl:template>
will probably fix this for your sample code but may have unexpected
side effects in more general cases.

J.Pietschmann

XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>