首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用xslt筛选每个不同元素和属性的第一次出现

如何使用xslt筛选每个不同元素和属性的第一次出现
EN

Stack Overflow用户
提问于 2021-08-19 05:01:13
回答 1查看 61关注 0票数 0

我试图保留具有唯一属性值的元素的第一个不同的出现。在下面的示例中,我们可以预测不同的值,但在实时场景中这是很困难的。

示例xml:

代码语言:javascript
复制
<?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是:

代码语言:javascript
复制
<?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为:

代码语言:javascript
复制
<?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 (可以是任意数字)。有没有办法编写通用代码来处理这种情况,而不是像..。

代码语言:javascript
复制
        <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]" />

提前谢谢,如果有什么想法也很有帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-19 05:24:47

你可以用grouping来解决这个问题。

XSLT2.0

代码语言:javascript
复制
<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>

Demohttps://xsltfiddle.liberty-development.net/pNEj9dN

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68842264

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档