xsl将取到的值当变量,遇到问题,请教!
有下面的XML:
想我想把每个catalogid对应的所有ChannelName取出来,虽然可以按CataLogId排序把所有ChannelName取出来,但我需要按每个catalogid去取。
-------类似这样的功能,但channel[CataLogId]要求从CatalogProperty/Catalog/catalogid中循环读取------
<?xml version= "1.0 " encoding= "gb2312 "?>
<xsl:stylesheet xmlns:xsl= "http://www.w3.org/1999/XSL/Transform " version= "1.0 ">
<xsl:template match= "/ ">
<xsl:for-each select= "root/channel[CataLogId= '12 '] ">
<xsl:value-of select= "ChannelName "/> <br/>
</xsl:for-each>
<br/>
<xsl:for-each select= "root/channel[CataLogId= '13 '] ">
<xsl:value-of select= "ChannelName "/> <br/>
</xsl:for-each>
----------XML--------
<?xml version= "1.0 " encoding= "gb2312 " ?>
<root>
<CatalogProperty>
<Catalog>
<catalogid> 12 </catalogid>
</Catalog>
<Catalog>
<catalogid> 13 </catalogid>
</Catalog>
<Catalog>
<catalogid> 14 </catalogid>
</Catalog>
<Catalog>
<catalogid> 15 </catalogid>
</Catalog>
</CatalogProperty>
<channel>
<CataLogId> 14 </CataLogId>
<Author> CEG </Author>
<ChannelName> myChannel1 </ChannelName>
</channel>
<channel>
<CataLogId> 14 </CataLogId>
<Author> dfsdf </Author>
<ChannelName> sfdwetewr </ChannelName>
</channel>
<channel>
<Author> CEG2 </Author>
<CataLogId> 15 </CataLogId>
<ChannelName> myChannel2 </ChannelName>
</channel>
<channel>
<Author> CEG2 </Author>
<CataLogId> 13 </CataLogId>
<ChannelName> myChannel2 </ChannelName>
</channel>
<channel>
<Author> CEG2 </Author>
<CataLogId> 15 </CataLogId>
<ChannelName> myChannel2 </ChannelName>
</channel>
<channel>
<Author> CEG12 </Author>
<CataLogId> 12 </CataLogId>
<ChannelName> myChannel12 </ChannelName>
</channel>
</root>
---------------------------
[解决办法]
xsl中的变量附值一次后就不能变的,楼主还是换个方法吧O.o
//想我想把每个catalogid对应的所有ChannelName取出来
可以采用分组的方法。
有两种方式,楼主可以参考http://www.hua2r.com/blog/ea47bd2a75606e70fba23e37ffa1b8f8.html
[解决办法]
<?xml version= "1.0 " encoding= "UTF-8 "?>
<xsl:stylesheet version= "1.0 " xmlns:xsl= "http://www.w3.org/1999/XSL/Transform ">
<xsl:output encoding= "UTF-8 " method= "html "/>
<xsl:template match= "CatalogProperty ">
<xsl:for-each select= "Catalog ">
<xsl:variable name= "id " select= "catalogid "/>
<xsl:apply-templates select= "/channel[@CataLogId=$id] "/>
</xsl:for-each>
</xsl:template>
<xsl:template match= "channel ">
<xsl:for-each select= "* ">
<xsl:value-of select= ". "/> |
</xsl:for-each>
<hr />
</xsl:template>
</xsl:stylesheet>