您的位置:首页 > 其它

xslt for-each 中,使用动态select

2013-01-01 22:11 387 查看
朋友发来一简单的xml和xsl,如下

<?xml version="1.0" encoding="utf-8" ?>

<?xml-stylesheet type="text/xsl" href="./xsl/test.xsl" ?>

<root point_id="1" point_name="海康嵌入式DVR51图像" week="6"
year="2012" month="12" day="23" >

<w_0 >

<r_0 start_hour="0" start_minute="0" stop_hour="0" stop_minute="0" ></r_0>

<r_1 start_hour="0" start_minute="0" stop_hour="0" stop_minute="0" ></r_1>

<r_2 start_hour="0" start_minute="0" stop_hour="0" stop_minute="0" ></r_2>

<r_3 start_hour="0" start_minute="0" stop_hour="0" stop_minute="0" ></r_3></w_0>

<w_1 >

<r_0 start_hour="0" start_minute="0" stop_hour="0" stop_minute="0" ></r_0>

<r_1 start_hour="0" start_minute="0" stop_hour="0" stop_minute="0" ></r_1>

<r_2 start_hour="0" start_minute="0" stop_hour="0" stop_minute="0" ></r_2>

<r_3 start_hour="0" start_minute="0" stop_hour="0" stop_minute="0" ></r_3></w_1>

</root>

XSLT 内容:

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="root">

<xsl:variable name="week" select='concat("w_",@week)'/>

<xsl:for-each
select="$week/*">

<!--do something here-->

</xsl:for-each>


希望xsl能够根据xml中week属性值动态获取子节点中对应的数据节点

运行时抛出

Expression does not return a DOM node. -->$week<-- 的错误

经过搜索,找到一下的解决方法,用下面代码替换即可上述红色代码

select="*[local-name() = $week]/*" 或者 select="*[contains(local-name(), $week)]/*

full dynamic XPath evaluation isn't part of either XSLT 1.0/XPath 1.0 or XSLT 2.0/XPath 2.0
http://stackoverflow.com/questions/7443426/dynamic-xslt-for-each-iteration
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: