[英]Use The Value Of An Element In XPATH Expression Like A Variable?
So I'm just trying to figure out whether its possible to select the value of element inside xpath like a variable? 所以我只是想弄清楚是否有可能像变量一样在xpath中选择element的值? Is this possible?
这可能吗? If not what are some user friendly method of achieving this?
如果不是,有什么用户友好的方法可以实现这一目标?
XML XML格式
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="grades.xsl"?>
<school>
<subject Id="3311">
<className>English</className>
<studentList>
<student id="1001">Lisa Simpson</student>
<student id="1002">Barney Rubble</student>
<student id="1003">Donald Duck</student>
</studentList>
<classwork>
<assignment name="Final Exam">
<mark studId="1001">38</mark>
<mark studId="1002">21</mark>
<mark studId="1003">20.5</mark>
</assignment>
</classwork>
</subject>
</school>
XSL XSL
<td><xsl:value-of select="@sid"/></td>
<td><xsl:value-of select="//assignment/mark[@studId='@sid']"/></td>
I suspect you real question is how to get the student's marks from the context of student
. 我怀疑你真正的问题是如何从上下文得到学生的痕迹
student
。
This can be done using: 可以使用以下方法完成:
<xsl:value-of select="//assignment/mark[@studId=current()/@id]"/>
A better answer is to define a key at the top level of your stylesheet as: 更好的答案是在样式表的顶级定义键 :
<xsl:key name="marks" match="mark" use="@studId" />
then use the expression key('mark', @id)
to get the related marks. 然后使用表达式
key('mark', @id)
获得相关标记。
For example, the following stylesheet: 例如,以下样式表:
XSLT 1.0 XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:key name="marks" match="mark" use="@studId" />
<xsl:template match="/school">
<table border="1">
<xsl:for-each select="subject/studentList/student">
<tr>
<td>
<xsl:value-of select="."/>
</td>
<td>
<xsl:value-of select="key('marks', @id)"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
applied to your input example, will show: 应用于您的输入示例,将显示:
Not that the examples assume each student has at most one mark. 这些示例并非假设每个学生最多获得一个分数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.