使用 XSLT 读取标签或元素的多个实例
我的 RDF xml 文件是这样的。
<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>
现在在我的 XSLT 样式表中,我需要检索所有 j.0:property
标记的值。我正在使用这样的东西:
<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>
但是它会返回相同的值 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在模板匹配内部,您位于匹配元素的上下文中。因此,如果您尝试获取属性的值,您所要做的就是:
您当前在 select 属性中使用的路径以“/”开头,因此是从文档根目录开始的路径,而不是相对于你所在的位置。无论在哪里使用,它总是返回相同的值。
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:
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.
不确定您显示的 XSLT 是否真的是您正在使用的 XSLT。按照你发布的方式,它无法编译。
xsl:apply-templates
行与xsl:template
行位于同一级别吗?xsl:text
真的包含xsl:value-of
吗?如果是这样,我很想知道您使用什么处理器,因为任何处理器都不应在不通知您错误的情况下处理您的 XSLT。也就是说,要改进样式表,请执行
使用 XSLT 1.0,如果您使用
xsl:value-of
选择多个节点,它将仅输出第一个。因为您似乎确实在某处有一个显然有效的xsl:apply-templates
,该行选择所有内容但仅返回第一个(代码中以/),将为
xsl:apply-templates
中选择的每个节点调用三次。为了进一步、更好地帮助您,请展示一个完整的 XSLT 样式表的最小示例,我们可以针对您的示例数据运行该样式表。
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 thexsl:template
line? Is thexsl:text
really containingxsl: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 yourxsl:text
error, you get this: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 anxsl: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 yourxsl: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:
When you get to hte template you're in the selected item so all you need is: