XSLT拼接XML中多个Subject term类marcEntry节点的问题求助
问题原因
XSLT 1.0中,xsl:value-of 匹配到多个节点时,只会返回第一个匹配节点的文本内容,concat 函数也不会自动遍历所有符合条件的节点,因此你原来的写法只能拿到第一个主题词,无法实现多主题词拼接的需求。
解决方法
将你XSLT中主题词对应的<td>块替换为如下代码,即可实现多个主题词用分号拼接的效果,同时兼容无主题词、最多6个主题词的场景:
<td align="right"> <xsl:for-each select="marc/marcEntry[@label='Subject term']"> <!-- 除第一个主题词外,其他主题词前加分号分隔 --> <xsl:if test="position() > 1"> <xsl:text>;</xsl:text> </xsl:if> <xsl:value-of select="."/> </xsl:for-each> </td>
如果需要拼接多个ISBN值,也可以用同样的遍历逻辑替换ISBN对应的<td>块即可。
修改后的完整XSLT代码
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/> <xsl:template match="/"> <html> <head> </head> <body> <table border="1"> <tr bgcolor="#9acd32"> <th>Call Number</th> <th>Title</th> <th>Author</th> <th>Subject</th> <th>ISBN</th> <th>Item ID</th> <th>Hegis</th> <th>Copy</th> <th>Location</th> </tr> <xsl:for-each select="report/catalog"> <tr> <td><xsl:value-of select="call/callNumber"/></td> <td><xsl:value-of select="marc/marcEntry[@label='Title']"/></td> <td align="right"><xsl:value-of select="marc/marcEntry[@label='Personal Author']"/></td> <td align="right"> <xsl:for-each select="marc/marcEntry[@label='Subject term']"> <xsl:if test="position() > 1"> <xsl:text>;</xsl:text> </xsl:if> <xsl:value-of select="."/> </xsl:for-each> </td> <td align="right"><xsl:value-of select="marc/marcEntry[@label='ISBN']"/></td> <td align="right"><xsl:value-of select="call/item/itemID"/></td> <td><xsl:value-of select="call/item/category1"/></td> <td><xsl:value-of select="call/item/copyNumber"/></td> <td><xsl:value-of select="call/item/homeLocation"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
内容的提问来源于stack exchange,提问作者Lorraine Botros
相关产品推荐
相关产品推荐

