开发者

Reading multiple instances of a tag or element using XSLT

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"/&g开发者_如何学Ct;

<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.


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.


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.


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" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜