我有以下
XML:
<research> <research.record> <research.record_number>1</research.record_number> <research.type> <value lang="en-US">some research type</value> </research.type> <research.type> <value lang="en-US">some other type of research</value> </research.type> <project.record> <priref>101</priref> <project.type> <value lang="en-US">some type of project</value> </project.type> </project.record> </research.record> </research> <research> <research.record> <research.record_number>2</research.record_number> <research.type> <value lang="en-US">some other type of research</value> </research.type> <research.type> <value lang="en-US">a third type of research</value> </research.type> <project.record> <priref>101</priref> <project.type> <value lang="en-US">some type of project</value> </project.type> </project.record> </research.record> </research> <research> <research.record> <research.record_number>3</research.record_number> <research.type> <value lang="en-US">some other type of research</value> </research.type> <research.type> <value lang="en-US">a fourth type</value> </research.type> <project.record> <priref>201</priref> <project.type> <value lang="en-US">some other type of project</value> </project.type> </project.record> </research.record> </research> <research> ... etc ...
使用XSLT 1.0,我使用xsl:key将此XML转换为唯一项目记录的列表.
到现在为止还挺好…
问题是:我还想为每个独特的项目记录显示独特的研究类型.
我的简化样式表显示了重复的research.types(但不是唯一的)
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> <xsl:output method="html" indent="yes"/> <xsl:key name="uniqueProject" match="project.record" use="priref"/> <xsl:template match="record"> <div class="table-row"> <xsl:apply-templates select="//project.record[generate-id() = generate-id(key('uniqueProject',priref)[1])]"> <xsl:sort select="startdate"/> </xsl:apply-templates> </div> </xsl:template> <xsl:template match="project.record"> <div class="project-container"> <xsl:text>project.record </xsl:text> <xsl:value-of select="priref"/> <xsl:text>: </xsl:text> <xsl:for-each select="//research/research.record/research.type[../project.record/priref = current()/priref]"> <xsl:sort select="value[@lang='en-US']" data-type="text" /> <xsl:if test="value[@lang='en-US'][not(.=preceding::research/research.record/research.type/value[@lang='en-US'][../../project.record/priref = current()/priref])]"> <xsl:value-of select="value[@lang='en-US']"/> </xsl:if> <xsl:if test="position()!=last()"> <xsl:text>,</xsl:text> </xsl:if> </xsl:for-each> </div> <br/> <br/> </xsl:template> </xsl:stylesheet>
我想要的输出是:
project.record 101: some research type,some other type of research,a third type of research project.record 201: some other type of research,a fourth type
希望有人可以帮助我使用正确的XSLT / XPATH. (只能使用XSLT1.0)
我会通过使用第二个键来解决这个问题,该键将research.type值组合在一起,它们的值和相关的priref组合在一起.
原文链接:https://www.f2er.com/xml/452474.html<xsl:key name="resTypeKey" match="research.type/value[@lang='en-US']" use="concat(.,'+++',../../project.record/priref)" />
然后使用与已有的表行相同的Muenchian技巧:
<xsl:template match="project.record"> <div class="project-container"> <xsl:text>project.record </xsl:text> <xsl:value-of select="priref"/> <xsl:text>: </xsl:text> <xsl:apply-templates select=" key('uniqueProject',priref)/../research.type /value[@lang='en-US'][ generate-id() = generate-id( key('resTypeKey',concat(.,current()/priref))[1])]"> <xsl:sort select="." /> </xsl:apply-templates> </div> <br/> <br/> </xsl:template> <xsl:template match="research.type/value"> <xsl:if test="position() > 1">,</xsl:if> <xsl:value-of select="."/> </xsl:template>