Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to get the sub sec title in XML

Status
Not open for further replies.

hpdp

Programmer
Dec 11, 2009
3
US
Hi, I have a xml document,

<body>
<sec sec-type="intro" id="S1">
<title>1. Introduction</title>
<p id="P2">The p2</p>
</sec>
<sec>
<title>2. method</title>
<sec>
<title>2.1 title</title>
<p>the 2.1 p</p>
<sec>
<sec>
</body>

Here's my XSLT

<xsl:template match="/">
<html>
<body bgcolor="#FFFFFF">
<xsl:apply-templates select="//body/sec"/>
</body>
</html>
</xsl:template>

<xsl:template match="//body/sec">
<center><font color="blue">
<br/><xsl:apply-templates select="title"/>
</font></center>
<br/>

<xsl:for-each select="descendant::p">
<xsl:apply-templates/>
<br/><br/>
</xsl:for-each>

<xsl:template select="descendant::sec">
<b><xsl:apply-templates select="title"/></b>
<xsl:for-each select="descendant::p">
<xsl:apply-templates/>
<br/><br/>
</xsl:for-each>
</xsl:template>

</xsl:template>

In the html output, I cannot see the sub sec title "2.1 title", though I got all the <p>.
How to get the sub sec title? Thanks!!
 
xsl:template can never be a child of another xsl:template.
 
Ok. I took the nested xsl:template out. My code is

<xsl:template match="/">
<html>
<body bgcolor="#FFFFFF">
<xsl:apply-templates select="//body/sec"/>
</body>
</html>
</xsl:template>

<xsl:template match="//body/sec">
<center><font color="blue">
<br/><xsl:apply-templates select="title"/>
</font></center>
<br/>

<xsl:for-each select="descendant::p">
<xsl:apply-templates/>
<br/><br/>
</xsl:for-each>
</xsl:template>

But I still cannot get the sub sec title "2.1 title".
Any suggestion? Thanks!!
 
The templates look superficially sophisticated but they are actually lack of clarity and hence covering some weakness hard to detect.

You have descendant::p. Why not put a descendant::title as well, because it is precisely the place to match title 2.1.
 
I tried descendant::title. It put all titles together before contents of all <p>. I also tried to add

<center><font color="blue">
<br/><xsl:apply-templates select="sec/title"/>
</font></center>
<br/>

it wrote out all sub sec titles before contents of all <p>.
Thanks!!
 
The problem is that the templates never speak out with clarity what the author of them has in mind the display layout. Maybe you can sketch and show the layout so that I may answer tomorrow if nobody steps in - I am not in your time zone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top