XML Development

Moderators: None (Apply to moderate this forum)
Number of threads: 252
Number of posts: 451

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
XSL question Posted by jnunes on 4 Feb 2003 at 1:49 PM
Hi.

When I try to view my para.xml file with the XSL stylesheet with IE5, I get something like

1st title

2nd title

1st paragraph

2nd paragraph

This format is not desirable. What I want is to get the 1st title and then the 1st paragraph, and only after that the 2nd title and the 2nd paragraph.

Does any one know how to do this only by modifying the XSL stylesheet?

Here are the files:

-------------------
para.xml file:

<?xml-stylesheet type="text/xsl" href="para.xsl"?>
<document>
<ptitle>1st title</ptitle>
<text>1st paragraph</text>
<ptitle>2nd title</ptitle>
<text>2nd paragraph</text>
</document>

-------------------
para.xsl file:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<HTML>
<BODY>
<xsl:for-each select="document">

<xsl:for-each select="ptitle">
<p><b><xsl:value-of select="text()"/></b></p>
</xsl:for-each>

<xsl:for-each select="text">
<p><xsl:value-of select="text()"/></p>
</xsl:for-each>
</xsl:for-each>
</BODY>
</HTML>
</xsl:template>

</xsl:stylesheet>
Report
Re: XSL question Posted by iDaZe on 5 Feb 2003 at 7:37 AM
This message was edited by iDaZe at 2003-2-5 7:38:8

: Hi.
:
: When I try to view my para.xml file with the XSL stylesheet with IE5, I get something like
:
: 1st title
:
: 2nd title
:
: 1st paragraph
:
: 2nd paragraph
:
: This format is not desirable. What I want is to get the 1st title and then the 1st paragraph, and only after that the 2nd title and the 2nd paragraph.
:
: Does any one know how to do this only by modifying the XSL stylesheet?
:
: Here are the files:
:
: -------------------
: para.xml file:
:
: <?xml-stylesheet type="text/xsl" href="para.xsl"?>
: <document>
: <ptitle>1st title</ptitle>
: <text>1st paragraph</text>
: <ptitle>2nd title</ptitle>
: <text>2nd paragraph</text>
: </document>
:
: -------------------
: para.xsl file:
:
: <?xml version="1.0"?>
: <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
: <xsl:template match="/">
: <HTML>
: <BODY>
: <xsl:for-each select="document">
:
: <xsl:for-each select="ptitle">
: <p><b><xsl:value-of select="text()"/></b></p>
: </xsl:for-each>
:
: <xsl:for-each select="text">
: <p><xsl:value-of select="text()"/></p>
: </xsl:for-each>
: </xsl:for-each>
: </BODY>
: </HTML>
: </xsl:template>
:
: </xsl:stylesheet>
:

I'm not really much of an expert on xml (only read a few tutorials and don't really use it much), but I don't think it can be done that easily.

It would be easier if you edit both documents:
<?xml-stylesheet type="text/xsl" href="para.xsl"?>
<document>
  <paragraph>
    <ptitle>1st title</ptitle>
    <text>1st paragraph</text>
  </paragraph>
  <paragraph>
    <ptitle>2nd title</ptitle>
    <text>2nd paragraph</text>
  </paragraph>
</document>


and

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<HTML>
<BODY>
    <xsl:for-each select="document/paragraph">
	    <p><b><xsl:value-of select="ptitle"/></b></p>
	    <p><xsl:value-of select="text"/></p>
    </xsl:for-each>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>


Report
Re: XSL question Posted by infidel on 3 Mar 2003 at 7:41 AM
This message was edited by infidel at 2003-3-3 7:43:22

This message was edited by infidel at 2003-3-3 7:41:54

: Hi.
:
: When I try to view my para.xml file with the XSL stylesheet with IE5, I get something like
:
: 1st title
:
: 2nd title
:
: 1st paragraph
:
: 2nd paragraph
:
: This format is not desirable. What I want is to get the 1st title and then the 1st paragraph, and only after that the 2nd title and the 2nd paragraph.
:
: Does any one know how to do this only by modifying the XSL stylesheet?
:
: Here are the files:
:
: -------------------
: para.xml file:
:
: <?xml-stylesheet type="text/xsl" href="para.xsl"?>
: <document>
: <ptitle>1st title</ptitle>
: <text>1st paragraph</text>
: <ptitle>2nd title</ptitle>
: <text>2nd paragraph</text>
: </document>
:
: -------------------
: para.xsl file:
:
: <?xml version="1.0"?>
: <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
: <xsl:template match="/">
: <HTML>
: <BODY>
: <xsl:for-each select="document">
:
: <xsl:for-each select="ptitle">
: <p><b><xsl:value-of select="text()"/></b></p>
: </xsl:for-each>
:
: <xsl:for-each select="text">
: <p><xsl:value-of select="text()"/></p>
: </xsl:for-each>
: </xsl:for-each>
: </BODY>
: </HTML>
: </xsl:template>
:
: </xsl:stylesheet>

The problem is the for-each loop. You would want to put the text for-each inside the ptitle for-each to get what you want, but that's kind of an ugly solution and doesn't utilize the flexibility and power of markup and templates. Try this:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">

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

    <xsl:template match="document">
        <HTML>
        <BODY>
        <xsl:apply-templates/>
        </BODY>
        </HTML>
    </xsl:template>

    <xsl:template match="ptitle">
	<h1><xsl:value-of select="."/></h1>
        <xsl:apply-templates/>
    </xsl:template>

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

</xsl:stylesheet>


I haven't tried it out but it should work. Should also show you how powerful templates can be. You can think of templates as a kind of function call. It's better to write a lot of small functions that can handle any input rather than a single monolithic function which has to be modified any time the input is modified.


infidel








 

Recent Jobs