Xml 在xslt中对多个相同节点内容进行分组
我正在创建一个XML和XSLT文件,以将XML文件中的一些数据显示为CSV。 我被困在一个特定的方面,因为我找不到许多具有类似xml结构的在线示例 考虑到这个假设的XML:Xml 在xslt中对多个相同节点内容进行分组,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我正在创建一个XML和XSLT文件,以将XML文件中的一些数据显示为CSV。 我被困在一个特定的方面,因为我找不到许多具有类似xml结构的在线示例 考虑到这个假设的XML: <collection> <book> <author> author1name </author> <author> author2name </author> <title> booktitle </title>
<collection>
<book>
<author> author1name </author>
<author> author2name </author>
<title> booktitle </title>
</book>
<book>
<author> authorname </author>
<title> booktitle </title>
</book>
</collection>
请注意,输出中不包含author2。这是数据的巨大损失。
我曾尝试使用嵌套for循环遍历所有作者,但遇到的错误太多,无法计数
有人能提出一种方法来产生
author1name;author2name, booktitle
第一本书?(两位作者之间用分号分隔)
感谢您的帮助。您可以创建一个与
作者
匹配的模板,您只需在其中输出名称,如果不是第一作者,则在名称前面加上分号
<xsl:template match="author">
<xsl:if test="position() > 1">;</xsl:if>
<xsl:value-of select="." />
</xsl:template>
试试这个XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
<xsl:template match="collection">
<xsl:text>author,title</xsl:text>
<xsl:value-of select="'
'" />
<xsl:apply-templates select="book"/>
</xsl:template>
<xsl:template match="book">
<xsl:apply-templates select="author"/>
<xsl:value-of select="concat(',', title,'
')"/>
</xsl:template>
<xsl:template match="author">
<xsl:if test="position() > 1">;</xsl:if>
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
concat()是一个字符串函数;不能将其应用于节点集。请尝试:
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/">
<xsl:text>author,title </xsl:text>
<xsl:for-each select="collection/book">
<xsl:for-each select="author">
<xsl:value-of select="."/>
<xsl:if test="position()!=last()">
<xsl:text>;</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>,</xsl:text>
<xsl:value-of select="title"/>
<xsl:if test="position()!=last()">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
作者,书名
;
;
,
<xsl:apply-templates select="author" />
<xsl:value-of select="concat(',', title,'
')"/>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
<xsl:template match="collection">
<xsl:text>author,title</xsl:text>
<xsl:value-of select="'
'" />
<xsl:apply-templates select="book"/>
</xsl:template>
<xsl:template match="book">
<xsl:apply-templates select="author"/>
<xsl:value-of select="concat(',', title,'
')"/>
</xsl:template>
<xsl:template match="author">
<xsl:if test="position() > 1">;</xsl:if>
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
<xsl:value-of select="author" separator=";" />
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/">
<xsl:text>author,title </xsl:text>
<xsl:for-each select="collection/book">
<xsl:for-each select="author">
<xsl:value-of select="."/>
<xsl:if test="position()!=last()">
<xsl:text>;</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>,</xsl:text>
<xsl:value-of select="title"/>
<xsl:if test="position()!=last()">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>