我试图保留具有唯一属性值的元素的第一个不同的出现。在下面的示例中,我们可以预测不同的值,但在实时场景中这是很困难的。
示例xml:
<?xml version="1.0" encoding="UTF-8"?>
<?xsl-stylesheet type="text/xsl" href="unique_first_occ.xsl" ?>
<Root>
<grade.content>
<head.block>
<codes.head md="hg2">
<head.info>
<label.name>ARTICLE</label.name>
<label.designator>1</label.designator>
</head.info>
</codes.head>
<codes.head md="hg2c">
<head.info>
<headtext>PUBLIC</headtext>
</head.info>
</codes.head>
<codes.head md="hg3">
<head.info>
<label.name>ARTICLE</label.name>
<label.designator>1</label.designator>
</head.info>
</codes.head>
<codes.head md="hg3c">
<head.info>
<headtext>PUBLIC</headtext>
</head.info>
</codes.head>
<codes.head md="hg3">
<head.info>
<label.name>ARTICLE</label.name>
<label.designator>1</label.designator>
</head.info>
</codes.head>
<codes.head md="hg3c">
<head.info>
<headtext>PUBLIC</headtext>
</head.info>
</codes.head>
</head.block>
</grade.content>
</Root>我的xslt是:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/Root">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="codes.head[@md='hg1'][position() > 1]" />
<xsl:template match="codes.head[@md='hg1c'][position() > 1]" />
<xsl:template match="codes.head[@md='hg2'][position() > 1]" />
<xsl:template match="codes.head[@md='hg2c'][position() > 1]" />
<xsl:template match="codes.head[@md='hg3'][position() > 1]" />
<xsl:template match="codes.head[@md='hg3c'][position() > 1]" />
</xsl:stylesheet>预期输出xml为:
<?xml version="1.0" encoding="UTF-8"?><?xsl-stylesheet type="text/xsl" href="unique_first_occ.xsl" ?>
<Root>
<grade.content>
<head.block>
<codes.head md="hg2">
<head.info>
<label.name>ARTICLE</label.name>
<label.designator>1</label.designator>
</head.info>
</codes.head>
<codes.head md="hg2c">
<head.info>
<headtext>PUBLIC</headtext>
</head.info>
</codes.head>
<codes.head md="hg3">
<head.info>
<label.name>ARTICLE</label.name>
<label.designator>1</label.designator>
</head.info>
</codes.head>
<codes.head md="hg3c">
<head.info>
<headtext>PUBLIC</headtext>
</head.info>
</codes.head>
</head.block>
</grade.content>
</Root>上面的xslt正常工作,但在实时情况下,这个hg*和hgc (可以是任意数字)。有没有办法编写通用代码来处理这种情况,而不是像..。
<xsl:template match="codes.head[@md='hg1'][position() > 1]" />
<xsl:template match="codes.head[@md='hg1c'][position() > 1]" />
<xsl:template match="codes.head[@md='hg2'][position() > 1]" />
<xsl:template match="codes.head[@md='hg2c'][position() > 1]" />
<xsl:template match="codes.head[@md='hg3'][position() > 1]" />
<xsl:template match="codes.head[@md='hg3c'][position() > 1]" />提前谢谢,如果有什么想法也很有帮助。
发布于 2021-08-19 05:24:47
你可以用grouping来解决这个问题。
XSLT2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="head.block">
<xsl:copy>
<xsl:for-each-group select="codes.head" group-by="@md">
<xsl:apply-templates select="current-group()[1]"/>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/68842264
复制相似问题