如何在XSLT中从更深层次访问根属性
我正在调用一个模板:如何在XSLT中从更深层次访问根属性,xslt,Xslt,我正在调用一个模板: <table> <xsl:apply-templates select="data/pics/row"/> </table> 模板是 <xsl:template match="row"> <tr> <xsl:for-each select="td"> <td border="0"> <a hr
<table>
<xsl:apply-templates select="data/pics/row"/>
</table>
模板是
<xsl:template match="row">
<tr>
<xsl:for-each select="td">
<td border="0">
<a href="{@referencddePage}">
<img src="{pic/@src}" width="{pic/@width}" height="{pic/@height}"/>
</a>
</td>
</xsl:for-each>
</tr>
</xsl:template>
我的XML是:
<?xml version="1.0" encoding="iso-8859-8"?>
<?xml-stylesheet type="text/xsl" href="xslFiles\smallPageBuilder.xsl"?>
<data pageNo="3" referencePage="xxxxxxxxxxxxxxx.xml">
<pics>
<row no="0">
<td col="0">
<pic src="A.jpg" width="150" height="120"></pic>
</td>
</row>
</pics>
</data>
我想要行:a hr e f=“{@referencedDepage}”
从中获取输入
根目录,:a h r e f=“{@referencedDepage}”…
,但我已经在
中使用一个XPATH,该XPATH使用一个前导斜杠“跳转”到文档顶部,然后沿着树向下走:
/data/@referencePage
将其应用于样式表:
<xsl:template match="row">
<tr>
<xsl:for-each select="td">
<td border="0">
<a href="{/data/@referencePage}">
<img src="{pic/@src}" width="{pic/@width}" height="{pic/@height}"/>
</a>
</td>
</xsl:for-each>
</tr>
</xsl:template>
我要那条线:一个h r e
f=“{@referencedDepage}”以获取
来自根目录的输入:a h r e f=
“{@referencedDepage}”。。。但是我
已在
如果规定@referencePage
属性始终是top元素的属性,则始终可以通过以下方式访问它:
/*/@referencePage
因此,在您的代码中,您将有:
<a href="{/*/@referencePage}">
在提供的XML文档上应用此转换时
<data pageNo="3" referencePage="xxxxxxxxxxxxxxx.xml">
<pics>
<row no="0">
<td col="0">
<pic src="A.jpg" width="150" height="120"></pic>
</td>
</row>
</pics>
</data>
产生所需的输出:
<tr>
<td border="0">
<a href="xxxxxxxxxxxxxxx.xml">
<img src="A.jpg" width="150" height="120"/>
</a>
</td>
</tr>
看看每个模板是如何非常简单的。此外,代码还进一步简化
现在,不是:
<img src="{pic/@src}" width="{pic/@width}" height="{pic/@height}"/>
我们只有:
<img src="{@src}" width="{@width}" height="{@height}"/>
好问题(+1)。请参阅我的答案,以获得一个既简单又完全符合XSLT精神的解决方案,主要使用“推式风格”。
<img src="{@src}" width="{@width}" height="{@height}"/>