在线:context()的作用????

jeskee 2003-10-14 11:53:11
在b.xsl中使用的context()有什么作用?"/bbs/post[@pid=context()/@sid]"它的判断是什么意思?
谢谢!
xsl文件:b.xsl
<?xml version="1.0" encoding="gb2312" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="*"/>
</body>
</html>
</xsl:template>
<xsl:template match="post">
<li>
<div>
<xsl:attribute name="title"><xsl:value-of select="content"/></xsl:attribute>
<xsl:attribute name="style">cursor:hand;</xsl:attribute>
<xsl:attribute name="onclick">javascript:alert("<xsl:value-of select="title"/>");</xsl:attribute>
<xsl:value-of select="title"/>
<xsl:if test="/bbs/post[@pid=context()/@sid]">
<xsl:element name="ul">
<xsl:apply-templates select="/bbs/post[@pid=context()/@sid]"/>
</xsl:element>
</xsl:if>
</div>
</li>
</xsl:template>
<xsl:template match="bbs">
<ul>
<xsl:apply-templates select="post[@pid=0]"/>
</ul>
</xsl:template>
</xsl:stylesheet>

xml文件:bbs.xml
<?xml version="1.0" encoding="gb2312" ?>
<?xml-stylesheet type="text/xsl" href="b.xsl" ?>
<bbs>
<post sid="4" pid="3" aid="1">
<title>4-3-1,this is a test</title>
<content>slddfjslajfsdljf</content>
</post>
<post sid="5" pid="3" aid="1">
<title>5-3-1,this is a test</title>
<content>slddfjslajfsdljf</content>
</post>
<post sid="3" pid="1" aid="1">
<title>3-1-1,this is a test</title>
<content>slddfjslajfsdljf</content>
</post>
<post sid="1" pid="0" aid="1">
<title>1-0-1,this is a test</title>
<content>slddfjslajfsdljf</content>
</post>
<post sid="2" pid="0" aid="2">
<title>2-0-2,this is a test</title>
<content>slddfjslajfsdljf</content>
</post>
</bbs><!--这里sid是贴子的id号,pid是贴子的父id号。title是标题,content是贴子的内容-->
...全文
41 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
useBook 2003-12-11
  • 打赏
  • 举报
回复
收藏
jeskee 2003-10-16
  • 打赏
  • 举报
回复
多谢,结贴!
zhfkiller 2003-10-15
  • 打赏
  • 举报
回复
context Method

[This is preliminary documentation and subject to change.]

Changes the context to the node from which this query started.

Syntax

context(index)

Parameters

index

Negative numbers indicate relative positions upward in the tree from the current context position. Natural numbers (zero and the positive integers) represent absolute positions working down from the root of the data tree. For example, context(0) is the root of the subtree upon which transformNode was called. context(1) is the context of the first context-switching XSL element in the style sheet (specified with the select attribute).

Remarks

In XSL, previous queries initiated by <xsl:for-each> can be obtained as well by using a numerical parameter.

The context method offers a way to create the most common type of joins. In particular, it provides a way to retrieve the context from which a query started. Thus, one can easily select nodes down a chain (or through links) where a property matches an attribute or other feature of the context from which the query started.

You can obtain elements returned by a previous query. You can also obtain indexes to nodes that might not otherwise be available.

The context method acts as a proxy for the context node. For example, the following will return the "a" attribute of the context node for the query:

context()/@a


The following will return all author nodes where the name is the same as the search attribute of the context:

//author[. = context()/@search]


The context method takes an optional integer argument. Negative numbers specify the number of context levels to walk up in the set of containing expressions relative to the current context: context(-1) represents the current context, context(-2) represents the containing context, and so on. The most commonly used argument is -1, which is the default value. context() is equivalent to context(-1).

In the following query, the inner for loop selects "b" nodes where the 'x' attribute is equal to the 'x' attribute of the context for the outer for loop. Given the sample data shown below, it returns the node x="1" corresponding to a="1" and the node x="2" when a="2". context(-2) specifies the containing context, here the selected "a" nodes.

<a x="1">
<b>
<c x="1"/>
<c x="2"/>
</b>
</a>
<a x="2">
<b>
<c x="1"/>
<c x="2"/>
</b>
</a>
<xsl:for-each select="a">
<xsl:for-each select="b">
<xsl:for-each select="c[@x = context(-2)/@x]">
...
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>


The following query will select all "a" elements from the root of the subtree the style sheet is transforming:

<xsl:for-each select="context(0)/a">
...
</xsl:for-each>


Contexts cannot be used within a union. For more information, see the reference documentation for the | operator in Set Operations.

Note the following limitations on the context method:

context can appear only in the leftmost part of a query.

Only context() and context(-1) can be used in conjunction with the | operator. All other context values are prohibited.

Example

The following example applies the XSL style sheet "style" to the XML data, "data". The style sheet includes the following fragment:

<xsl:for-each select="data/classes">
<xsl:for-each select="class">
<xsl:for-each select="teacher">
<xsl:value-of
select="context(0)/data/teachers/teacher[@tid = context(-1)/@tid]"/>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>


context indicates the root of the data tree, so that query is fairly straightforward, walking down from the root to the teacher elements. context(-1) indicates the current context, so the subquery [@tid = context(-1)/@tid] selects only those teacher elements that match the teacher id (tid) values specified in the current context (as can be seen in the nested <xsl:for-each> elements) data/classes/class/teacher.

<HTML>
<HEAD>
<STYLE>
. {font-size:20pt}
</STYLE>
<SCRIPT>
function init() {
results.innerHTML = "";
res = data.transformNode(style.selectSingleNode("*"));
results.innerHTML = res;

}
</SCRIPT>
</HEAD>
<BODY onload="init()">
<XML id="data">
<data>
<classes mingrade="B">
<class nid="1">
<teacher tid="1"/>
<student sid="1"/>
<student sid="2"/>
<student sid="3"/>
</class>
<class nid="2">
<teacher tid="1"/>
<teacher tid="2"/>
<student sid="4"/>
<student sid="2"/>
<student sid="3"/>
</class>
</classes>

<teachers>
<teacher tid="1">Teresa Atkinson</teacher>
<teacher tid="2">Steve DeBroux</teacher>
</teachers>

<students>
<student sid="1"><name>Kathie Flood</name><grade>A</grade></student>
<student sid="2"><name>William Herlan</name><grade>B</grade></student>
<student sid="3"><name>Paul West</name><grade>D</grade></student>
<student sid="4"><name>Wendy Wheeler</name><grade>C</grade></student>
</students>
</data>
</XML>
<!--The XSL style sheet-->
<XML id="style">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<xsl:for-each select="data/classes">
<xsl:for-each select="class">
<DIV>Class <xsl:value-of select="@nid"/></DIV>
<HR/>
<P>Teacher(s)
<xsl:for-each select="teacher">
<DIV>
<xsl:value-of select="context(0)/data/teachers/teacher[(@tid = context(-1)/@tid)]"/>
</DIV>
</xsl:for-each>
</P>
<BR/>
<P>Student(s)
<xsl:for-each select="student">
<DIV>
<xsl:value-of select="context(0)/data/students/student[(@sid =
context(-1)/@sid) and (grade $le$ context(-3)/@mingrade)]/name"/>
</DIV>
</xsl:for-each>
</P>
<HR noshade="true" color="red"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
</XML>
<DIV id="results"></DIV>
</BODY>
</HTML>
孟子E章 2003-10-14
  • 打赏
  • 举报
回复
The context() method provides a mechanism for testing the position of a node in the context of a query.

8,906

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧