检查 OUTPUT 中的重复元素 [英] Checking for a duplicate element in the OUTPUT

查看:27
本文介绍了检查 OUTPUT 中的重复元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 XML,例如,它看起来像这样:

I've got some XML, for example purposes it looks like this:

<root>
    <field1>test</field1>
    <f2>t2</f2>
    <f2>t3</f2>
</root>

我想用 XSLT 转换它,但我想抑制输出中的第二个 f2 元素 - 当源中的第二个 f2 元素出现时,我如何检查我的模板以查看输出中是否已经存在 f2 元素被处理了吗?我的 XSLT 目前看起来像这样:

I want to transform it with XSLT, but I want to suppress the second f2 element in the output - how do I check inside my template to see if the f2 element already exists in the output when the second f2 element in the source is processed? My XSLT looks something like this at present:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="no" omit-xml-declaration="yes" standalone="no" />
  <xsl:template match="/">
    <xsl:for-each select="./root">
      <output>
        <xsl:apply-templates />        
      </output>
    </xsl:for-each>
  </xsl:template>
  <xsl:template match="*" >
      <xsl:element name="{name(.)}">
        <xsl:value-of select="." />
      </xsl:element>
  </xsl:template>
</xsl:stylesheet>

我认为我需要对模板中的 xsl:element 进行某种检查,但我不确定如何查询输出文档以查看该元素是否已经存在.

I need to do some sort of check around the xsl:element in the template I think, but I'm not sure how to interrogate the output document to see if the element is already present.

忘记了 pre 标签,现在应该可以看到代码了!

Forgot the pre tags, code should be visible now!

推荐答案

这取决于您想要的系统范围.

It depends how system wide you want to be.

即您是否只关心属于同一父级的子元素,还是同一级别的所有元素(如果您愿意,可以使用表亲")或文档中任何位置的元素...

i.e. Are you only concerned with elements that are children of the same parent, or all elements at the same level ('cousins' if you like) or elements anywhere in the document...

在第一种情况下,您可以检查前同级轴以查看是否存在其他同名元素.

In the first situation you could check the preceding-sibling axis to see if any other elements exist with the same name.

<xsl:if test="count(preceding-sibling::node()[name()=name(current())])=0">
  ... do stuff in here.
</xsl:if>

这篇关于检查 OUTPUT 中的重复元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆