我是XSLT 1.0的新手,我拥有下面的XML,我们将其传递到XSLT中以生成PDF。
XML:
<Family>
<Description>General psychiatry</Description>
</Family>
<Family>
<Description>Liaison Psychiatry</Description>
<Parent>General psychiatry</Parent>
</Family>
<Family>
<Description>Old age psychiatry</Description>
</Family>
<Family>
<Description>General psychiatry</Description>
</Family>
<Family>
<Description>Liaison</Description>
<Parent>General psychiatry</Parent>
</Family>
情况1: 第一个家庭标签包含父名称(描述),第二个家庭标签包含子名称(Decription)和父名称(父母)。
情况2: 只有第一个家庭标签具有父名称(描述),但是没有第二个家庭标签。
现在,我需要根据父子/子项对以上内容进行分组,我想到了使用MUENCHIAN方法,但这需要所有Family-tag在其下都具有相同的子标签,并且我无法更改以及XML格式。
任何克服基于家庭标签的上述两个案例和小组的建议都是最受赞赏的。 谢谢
答案 0 :(得分:0)
在 XSLT 2.0 中,您可以使用for-each-group
<xsl:template match="root">
<xsl:copy>
<xsl:for-each-group select="//Family[count(*) = 1][Description]" group-by=".">
<xsl:copy>
<xsl:copy-of select="Description"/>
<xsl:if test="//Family[Parent = current-group()[1]/Description]">
<SubFamily>
<xsl:for-each select="//Family[Parent = current-group()[1]/Description]">
<xsl:copy-of select="Description"/>
</xsl:for-each>
</SubFamily>
</xsl:if>
</xsl:copy>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
请参见https://xsltfiddle.liberty-development.net/3NSTbfe
在 XSLT 1.0 中使用MUENCHIAN方法
<xsl:key name="parent" match="Family" use="Description"/>
<xsl:key name="child" match="Family" use="Parent"/>
<xsl:template match="root">
<xsl:copy>
<xsl:for-each select="Family[not(Parent)][generate-id() = generate-id(key('parent', Description)[1])]">
<xsl:copy>
<xsl:copy-of select="Description"/>
<xsl:if test="//Family[Parent = current()/Description]">
<SubFamily>
<xsl:for-each select="//Family[Parent = current()/Description]">
<xsl:copy-of select="Description"/>
</xsl:for-each>
</SubFamily>
</xsl:if>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>