views:

48

answers:

3

My RDF xml file is something like this..

<rdf:RDF>
    <rdf:Description rdf:about="........">
        <j.0:property rdf:resource="....."/>
        <j.0:property rdf:resource=....."/>
        <j.0:property rdf:resource="........"/>
    </rdf:Description>
</rdf:RDF>

Now in my XSLT stylesheet I need to retrieve the values of all the j.0:property tags. I am using something like this:

<xsl:apply-templates select="j.0:property"/>

<xsl:template match="j.0:property">
      <xsl:text><xsl:value-of select="/rdf:RDF/rdf:Description/j.0:propert  /@rdf:resource"/></xsl:text>    
</xsl:template>

But then it returns the same value 3 times. The value being the value of the first property encountered. Kindly help as to how I can get the value for each property.

+4  A: 

Inside of a template match, you are within the context of the element that is matched. Therefore, if you are trying to get the value of an attribute, all you have to do is:

<xsl:value-of select="@rdf:resource"/>

The path you are currently using in your select attribute starts with "/" and is therefore a path starting at the root of the document instead of relative to where you are. It will always return the same value no matter where it is used.

Russell Leggett
THANK yOU!!!!it worked...awesome!!
shashank saket
This refers to the <xsl:value-of> tag, where indeed the question does not use a relative path, but an absolute one, resulting in the first match being used for the value-of.
Paul de Vrieze
Note that `xsl:value-of` cannot be used inside an `xsl:text`, as I also explained in my answer. The line as shown in the q. cannot compile.
Abel
A: 

This line is wrong:

<xsl:text><xsl:value-of select="/rdf:RDF/rdf:Description/j.0:propert  /@rdf:resource"/></xsl:text>

When you get to hte template you're in the selected item so all you need is:

<xsl:value-of select="@rdf:resource" />
Murph
The line is indeed wrong. But the solution (unfortunately) too: inside `xsl:text` you cannot have an `xsl:value-of` statement (or any element, for that matter).
Abel
thank you so much all of you!!
shashank saket
@Abel - Doh, too early in the day and my xslt is rusty. Now fixed.
Murph
+1  A: 

It's not sure whether the XSLT you show is really the XSLT you are using. The way you post it, it cannot compile. Is the xsl:apply-templates line on the same level as the xsl:template line? Is the xsl:text really containing xsl:value-of? If so, I'd be very interested in knowing what processor you use, because no processor should process your XSLT without informing you about the errors.

That said, to improve your stylesheet, do as Russel Leggett explains in his answer. Instead of selecting all nodes inside your template (you start out with a /, selecting from the root), select relatively from the current node. Taking his answer and removing your xsl:text error, you get this:

<xsl:template match="j.0:property">
    <xsl:value-of select="@rdf:resource"/>
</xsl:template>

Using XSLT 1.0, if you select multiple nodes with xsl:value-of it will output only the first. Because you do seem to have an xsl:apply-templates somewhere that is apparently working, that line, which selects all but returns only the first (the one in your code starting with /), will be called three times for each node selected in your xsl:apply-templates.

To help you further, and better, please show a minimal example of a full XSLT stylesheet that we can run against your sample data.

Abel