anyone here good with xpath and xsl?

Soldato
Joined
1 Feb 2006
Posts
8,188
basically the story is this..

im working with an xml code fragment as follows

Code:
<chapter number="1">
   <section>
       <table number="1">
...
<chapter number="2">
   <section>
       <table number="1">
...
<

(This is just a brief segment of the code I have in xml file.)

I am using an xsl file to apply templates to the xml file.

I want to add a label to my tables i.e. Table 1.1 based on the chapter number and the associated table number.

Basically what I need to know is how I can access the attribute "number" in the chapter element using xpath?

My template file will be something like

Code:
<xsl:text>Table</xsl:text>
<xsl:value-of select"xpath_expression1_goes_here" />
<xsl:text>.</xsl:text>
<xsl:value-of select"xpath_expression2_goes_here" />


I tried it as something like //chapter/@number but this kept returning the value "1" to me even though the figure I was studying was actually located in chapter "2"


Any help on this would be great!

Cheers
 
Assuming the following input file (I added a root node to get it to parse):

Code:
<book>
<chapter number="1">
   <section>
       <table number="1"/>
   </section>
</chapter>
		
<chapter number="2">
   <section>
       <table number="1"/>
   </section>
</chapter>
</book>

The following XSL will output the chapter.table numbers with a <br/> after each one:

Code:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >

<xsl:template match="book/chapter">
	Table <xsl:value-of select="@number"/>.<xsl:value-of select="section/table/@number" /><br/>
</xsl:template>

</xsl:transform>
 
hi cheers for the help.. slight problem though

I already have a template called "book" and it dumps out all of the content.

When I use match="book/chapter" it appears to crash and leave out a lot of the text!

Any ideas how to get around this?
 
If you have a template called "book" then simply do:

Code:
<xsl:template match="book">
	....book template here
	<xsl:template match="chapter">
		Table <xsl:value-of select="@number"/>.<xsl:value-of select="section/table/@number" /><br/>
	</xsl:template>
</xsl:template>
 
If you already have a template called "chapter", the best thing to do would be a for-each:

Code:
<xsl:for-each select="book/chapter">
	Table <xsl:value-of select="@number"/>.<xsl:value-of select="section/table/@number" /><br/>
</xsl:for-each>

Therein is the "problem" with XSLT/XPath, there's about 20 ways of doing the same thing, the for-each is probably the better way of doing this though. Maybe...

:)
 
really appreciate info! however, still isnt working for me!

I'll have a play around and see what happens and report back any progress.

Thanks a lot
J
 
also I want to include code snippets in my xml file:


I was using something like

Code:
<code_listing>
     <item>
     <item>
     <item>

.....


each new line being an <item> tag.

How can I add tabbing to my code so that it indents like a normal code snippet would?

I think I could maybe use character references or entity references but not sure how to do this.

Anyone any ideas?
 
Back
Top Bottom