Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XML动态删除重复的嵌套节(在XSLT或PHP中)_Xml_Xslt - Fatal编程技术网

XML动态删除重复的嵌套节(在XSLT或PHP中)

XML动态删除重复的嵌套节(在XSLT或PHP中),xml,xslt,Xml,Xslt,给定一个看起来像这样的XML- <personseRequest> <Row> <ServiceLocations> <ServiceLocation> <LocationId>s</LocationId> <Parameters> <Parameter> <Name&

给定一个看起来像这样的XML-

<personseRequest>
    <Row>
      <ServiceLocations>
        <ServiceLocation>
          <LocationId>s</LocationId>
          <Parameters>
            <Parameter>
              <Name>s</Name>
            </Parameter>
            <Parameter>
              <Name>s</Name>
            </Parameter>
          </Parameters>
        </ServiceLocation>
        <ServiceLocation>
          <LocationId>s</LocationId>
          <Parameters>
            <Parameter>
              <Name>s</Name>
            </Parameter>
            <Parameter>
              <Name>s</Name>
            </Parameter>
          </Parameters>
        </ServiceLocation>
      </ServiceLocations>
    </Row>
    <Row>
      <ServiceLocations>
        <ServiceLocation>
          <LocationId>s</LocationId>
          <Parameters>
            <Parameter>
              <Name>s</Name>
            </Parameter>
            <Parameter>
              <Name>s</Name>
            </Parameter>
          </Parameters>
        </ServiceLocation>
        <ServiceLocation>
          <LocationId>s</LocationId>
          <Parameters>
            <Parameter>
              <Name>s</Name>
            </Parameter>
            <Parameter>
              <Name>s</Name>
            </Parameter>
          </Parameters>
        </ServiceLocation>
      </ServiceLocations>
    </Row>
  <gateways>
    <Row>
      <servicename>s</servicename>
    </Row>
    <Row>
      <servicename>s</servicename>
    </Row>
  </gateways>
</personsRequest>

s
s
s
s
s
s
s
s
s
s
s
s
s
s
我需要删除所有重复的部分以获得以下输出-

<personseRequest>
    <Row>
      <ServiceLocations>
        <ServiceLocation>
          <LocationId>s</LocationId>
          <Parameters>
            <Parameter>
              <Name>s</Name>
            </Parameter>
          </Parameters>
        </ServiceLocation>
      </ServiceLocations>
    </Row>
  <gateways>
    <Row>
      <servicename>s</servicename>
    </Row>
  </gateways>
</personsRequest>

s
s
s
注意,这需要动态工作,因为标记名可能不同。 我曾尝试通过DOMXPath在PHP中实现这一点,但我无法动态完成。。。。 如前所述,XML可以包含任何名称标记或嵌套名称标记,我需要删除所有重复的部分。
谢谢。

使用身份转换加上一个空模板,用于那些在前面有
深度相等
的元素,以消除重复项可能会起作用,但会相当昂贵,我认为:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output indent="yes"/>

  <xsl:template match="*[some $prev-sibling in preceding-sibling::* satisfies deep-equal(., $prev-sibling)]"/>

</xsl:stylesheet>

他们有什么共同点才能被认为是重复的?你指的是任何级别的重复吗?以您的示例为例,是否只需要删除重复的行,或者说,如果一个参数包含两个相同的名称,则只需要删除重复的名称?在我处理的XML中,所有数据集都是重复的,这些数据集就是我要挑出的数据集。因此,在上面的示例中,重复的是“行”、“服务位置”和“参数”数据集。所以,是的,我需要在任何级别删除重复项。您可以使用哪个版本的XSLT?XML是否至少没有混合内容(即,是否确保它没有包含文本和元素内容的元素,例如
10 m
)?我对版本1或2没有意见。XML没有混合内容。谢谢!这在99%时有效。我目前唯一的问题是,当出现类似-------------------------------------------------------的情况时,您的代码将其转换为---------------------------------------------当我希望它为---------------------------------------------时@DanAbittan,从你的评论中不清楚遗漏了什么,我不知道如何解释
------------------------------------
的东西,
在语法上也是不可能的。考虑一个新的问题,你可以把必要的细节放在一个格式良好的代码中。抱歉,我不能在评论中缩进,所以这里是XML,你的代码把它变成了我想要的@ DanAbittan,正如我已经说过的,<代码> <代码>对我来说是没有意义的(你不能打开一个标签(例如<代码> <代码>)然后关闭另一个问题(例如,
),编辑您的问题或询问一个新问题,您可以在其中以格式良好的方式显示代码示例。
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>