Xml 选择attribute=“X”所在的元素
我有以下XMLXml 选择attribute=“X”所在的元素,xml,xslt,Xml,Xslt,我有以下XML <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="example3.xsl"?> <pics> <page no="1"> <pic> <td> <no src="http://www.templetons.com/brad/pq/pla
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="example3.xsl"?>
<pics>
<page no="1">
<pic>
<td>
<no src="http://www.templetons.com/brad/pq/playaphone.jpg" width="150" height="120">1</no>
</td>
</pic>
<pic>
<td>
<no src="http://motherjones.com/files/legacy/mojoblog/funny-cats-a10.jpg" width="150" height="120">4</no>
</td>
</pic>
</page>
<page no="2">
<pic>
<td>
<no src="http://motherjones.com/files/legacy/mojoblog/funny-cats-a10.jpg" width="150" height="120">4</no>
</td>
</pic>
<pic>
<td>
<no src="http://www.templetons.com/brad/pq/playaphone.jpg" width="150" height="120">1</no>
</td>
</pic>
</page>
</pics>
我希望使用XSL文件只选择一个页面
这一个给了我两个:
<xsl:for-each select="pics/page/pic">
<tr>
<xsl:for-each select="td">
<td><img>
<xsl:attribute name="src">
<xsl:value-of select="no//@src"/>
</xsl:attribute>
<xsl:attribute name="width">
<xsl:value-of select="no//@width"/>
</xsl:attribute>
<xsl:attribute name="height">
<xsl:value-of select="no//@height"/>
</xsl:attribute>
</img></td>
</xsl:for-each>
</tr>
</xsl:for-each>
在何处以及如何过滤/选择或寻址no=x属性
感谢Asaf诸如此类:
<xsl:for-each select="pics/page[1]/pic">
它应该选择第一个。如果要选择no=1的选项,可以执行以下操作:
<xsl:for-each select="pics/page[@no='1']/pic">
您可以使用[@att='val']对属性进行筛选: 更改此项: 为此: 并将处理添加到模板中:
<xsl:template match="pic[@no='2']"/>
然后,要排除特定图片(如第二张),请添加此空模板:
<xsl:template match="pic[@no='2']"/>
不要使用那些xsl:attribute。在您的情况下,可以像使用文本结果AVT一样使用xsl:copy of select=no/@*。另外,不要使用//来选择属性,只是//@src不起作用。有关一个好的简单解决方案,请参阅我的答案。不要使用-尽量不要使用,尽量避免使用。学点东西…亚历杭德罗和迪米特里·诺瓦切夫,谢谢你的提示。我正处于两项新技术的中间。目前我对xslt的需求非常基本。我的代码看起来是这样的,因为这是我可以从人们给我的示例中编译的最简单的代码。当你说不要使用某些东西时,它是基于经验的。分享这些知识可以帮助许多其他人,这些人需要的不仅仅是让它工作起来。。。当我有更多的时间时,我想接受你们的建议,学习更多:我非常感谢你们两位
<xsl:for-each select="pics/page/pic">
<tr>
<xsl:for-each select="td">
<td><img>
<xsl:attribute name="src">
<xsl:value-of select="no//@src"/>
</xsl:attribute>
<xsl:attribute name="width">
<xsl:value-of select="no//@width"/>
</xsl:attribute>
<xsl:attribute name="height">
<xsl:value-of select="no//@height"/>
</xsl:attribute>
</img></td>
</xsl:for-each>
</tr>
</xsl:for-each>
<xsl:apply-templates select="pics/page/pic"/>
<xsl:template match="pic">
<tr>
<xsl:for-each select="td">
<td>
<img src="{no/@src}" width="{no/@width}" height="{no/@height}"/>
<td>
</xsl:for-each>
</tr>
</xsl:template>
<xsl:template match="pic[@no='2']"/>