积极答复者
深度限定取值

问题
-
for-each可以这样用:<xsl:for-each select="catalog/cd[artist='Bob Dylan']">也就是说它只给最后遍历的节点加了属性限定。
但是请看:
<TestResults xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:c="http://www.w3.org/1999/XSL/asjkasklgh" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<Personnel>
<SystemOperator ID="administrator" name="administrator"/>
</Personnel>
<ResultSet ID="206" startDateTime="2006-08-20T13:15:37" endDateTime="2006-08-20T13:16:02">
<Outcome value="Failed"/>
<TestGroup ID="206" startDateTime="2006-08-20T13:15:37" endDateTime="2006-08-20T13:16:02">
<Outcome value="Failed"/>
<TestGroup ID="207" startDateTime="2006-08-20T13:15:37" endDateTime="2006-08-20T13:16:00">
<Outcome value="Failed"/>
<Test ID="208" startDateTime="2006-08-20T13:15:37" endDateTime="2006-08-20T13:15:43">
<Outcome value="Passed"/>
<TestResult ID="Numeric">
<TestData>
<c:Datum value="13.354571" xsi:type="c:double" nonStandardUnit="kOhm"/>
</TestData>
<TestLimits>
<Limits>
<c:SingleLimit comparator="GE">
<c:Datum nonStandardUnit="kOhm" value="3.000000" xsi:type="c:double"/>
</c:SingleLimit>
</Limits>
</TestLimits>
</TestResult>
</Test>
</TestGroup>
</TestGroup>
</ResultSet>
</TestResults>
我要取出:TestResults/ResultSet/TestGroup/TestGroup/Test路径下ID的值,
前提是:TestResults/ResultSet/TestGroup/TestGroup/Test/TestResult/TestLimits/Limits路径下必须是c:SingleLimit元素。
下面是我的两种写法:
一:<xsl:for-each select="TestResults/ResultSet/TestGroup/TestGroup/Test[TestResult/TestLimits/Limits='c:SingleLimit']">
<td><xsl:value-of select="@ID"/></td>
</xsl:for-each>
二:<xsl:for-each select="//c:SingleLimit">
<td><xsl:value-of select="c:SingleLimit..Limits..TestLimits..TestResult../@ID"/></td>
</xsl:for-each>
都不能取出ID的值,请教高手!!!!!!!!!!!
答案
-
没有任何困难啊。加上名称空间就可以了。
注意:你需要理解名称的作用是干什么的。
完整的例子【注意:你原来的xml文件有一点点问题,少写了一个i】,拷贝即可看到结果
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="1.xsl"?> <TestResults xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:c="http://www.w3.org/1999/XSL/asjkasklgh" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:fn="http://www.w3.org/2005/xpath-functions"> <Personnel> <SystemOperator ID="administrator" name="administrator"/> </Personnel> <ResultSet ID="206" startDateTime="2006-08-20T13:15:37" endDateTime="2006-08-20T13:16:02"> <Outcome value="Failed"/> <TestGroup ID="206" startDateTime="2006-08-20T13:15:37" endDateTime="2006-08-20T13:16:02"> <Outcome value="Failed"/> <TestGroup ID="207" startDateTime="2006-08-20T13:15:37" endDateTime="2006-08-20T13:16:00"> <Outcome value="Failed"/> <Test ID="208" startDateTime="2006-08-20T13:15:37" endDateTime="2006-08-20T13:15:43"> <Outcome value="Passed"/> <TestResult ID="Numeric"> <TestData> <c:Datum value="13.354571" xsi:type="c:double" nonStandardUnit="kOhm"/> </TestData> <TestLimits> <Limits> <c:SingleLimit comparator="GE"> <c:Datum nonStandardUnit="kOhm" value="3.000000" xsi:type="c:double"/> </c:SingleLimit> </Limits> </TestLimits> </TestResult> </Test> </TestGroup> </TestGroup> </ResultSet> </TestResults>
1.xsl
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:c="http://www.w3.org/1999/XSL/asjkasklgh" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:fn="http://www.w3.org/2005/xpath-functions"> <xsl:template match="/"> <xsl:for-each select="/TestResults/ResultSet/TestGroup/TestGroup/Test[TestResult/TestLimits/Limits/c:SingleLimit]/@ID"> <xsl:value-of select="."/> </xsl:for-each> </xsl:template> </xsl:stylesheet>
【孟子E章】- 已标记为答案 孟宪会Moderator 2009年6月16日 1:08
全部回复
-
没有任何困难啊。加上名称空间就可以了。
注意:你需要理解名称的作用是干什么的。
完整的例子【注意:你原来的xml文件有一点点问题,少写了一个i】,拷贝即可看到结果
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="1.xsl"?> <TestResults xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:c="http://www.w3.org/1999/XSL/asjkasklgh" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:fn="http://www.w3.org/2005/xpath-functions"> <Personnel> <SystemOperator ID="administrator" name="administrator"/> </Personnel> <ResultSet ID="206" startDateTime="2006-08-20T13:15:37" endDateTime="2006-08-20T13:16:02"> <Outcome value="Failed"/> <TestGroup ID="206" startDateTime="2006-08-20T13:15:37" endDateTime="2006-08-20T13:16:02"> <Outcome value="Failed"/> <TestGroup ID="207" startDateTime="2006-08-20T13:15:37" endDateTime="2006-08-20T13:16:00"> <Outcome value="Failed"/> <Test ID="208" startDateTime="2006-08-20T13:15:37" endDateTime="2006-08-20T13:15:43"> <Outcome value="Passed"/> <TestResult ID="Numeric"> <TestData> <c:Datum value="13.354571" xsi:type="c:double" nonStandardUnit="kOhm"/> </TestData> <TestLimits> <Limits> <c:SingleLimit comparator="GE"> <c:Datum nonStandardUnit="kOhm" value="3.000000" xsi:type="c:double"/> </c:SingleLimit> </Limits> </TestLimits> </TestResult> </Test> </TestGroup> </TestGroup> </ResultSet> </TestResults>
1.xsl
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:c="http://www.w3.org/1999/XSL/asjkasklgh" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:fn="http://www.w3.org/2005/xpath-functions"> <xsl:template match="/"> <xsl:for-each select="/TestResults/ResultSet/TestGroup/TestGroup/Test[TestResult/TestLimits/Limits/c:SingleLimit]/@ID"> <xsl:value-of select="."/> </xsl:for-each> </xsl:template> </xsl:stylesheet>
【孟子E章】- 已标记为答案 孟宪会Moderator 2009年6月16日 1:08