使用XSLT删除具有相同名称的XML层次结构元素?

问题描述 投票:1回答:1

我得到的XML响应在响应中具有相同的命名元素,导致我出现问题,我需要使用XSLT 1.0删除此重复元素。响应中的元素是<RoundIncidents>。我已经查看了其他问题,但无法找到将响应转换为正确格式所需的内容。

具有重复元素的XML响应

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap:Body>
      <GetSiteIncidentsResponse xmlns="http://webservices.whitespacews.com/">
         <GetSiteIncidentsResult>
            <ErrorCode>0</ErrorCode>
            <ErrorDescription>Success</ErrorDescription>
            <SuccessFlag>true</SuccessFlag>
            <RoundIncidents>
               <RoundIncidents>
                  <ExtensionData />
                  <AccountSiteID>0</AccountSiteID>
                  <RoundIncidentID>8</RoundIncidentID>
                  <RoundRoundAreaServiceScheduleID>157</RoundRoundAreaServiceScheduleID>
                  <RoundCode>REC1</RoundCode>
                  <ScheduleName>MonFort2</ScheduleName>
                  <ServiceName>Recycling Collection Service</ServiceName>
                  <RoundAreaName>REC1 - MonFort2</RoundAreaName>
                  <RoundIncidentDate>2019-04-08T16:12:10</RoundIncidentDate>
                  <RoundIncidentNotes>Road Closed</RoundIncidentNotes>
                  <RoundIncidentCreatedByID>129</RoundIncidentCreatedByID>
                  <RoundIncidentCreatedDate>2019-04-08T16:12:26.493</RoundIncidentCreatedDate>
               </RoundIncidents>
            </RoundIncidents>
         </GetSiteIncidentsResult>
      </GetSiteIncidentsResponse>
   </soap:Body>
</soap:Envelope>

XSLT转换后的预期结果

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap:Body>
      <GetSiteIncidentsResponse xmlns="http://webservices.whitespacews.com/">
         <GetSiteIncidentsResult>
            <ErrorCode>0</ErrorCode>
            <ErrorDescription>Success</ErrorDescription>
            <SuccessFlag>true</SuccessFlag>            
               <RoundIncidents>
                  <ExtensionData />
                  <AccountSiteID>0</AccountSiteID>
                  <RoundIncidentID>8</RoundIncidentID>
                  <RoundRoundAreaServiceScheduleID>157</RoundRoundAreaServiceScheduleID>
                  <RoundCode>REC1</RoundCode>
                  <ScheduleName>MonFort2</ScheduleName>
                  <ServiceName>Recycling Collection Service</ServiceName>
                  <RoundAreaName>REC1 - MonFort2</RoundAreaName>
                  <RoundIncidentDate>2019-04-08T16:12:10</RoundIncidentDate>
                  <RoundIncidentNotes>Road Closed</RoundIncidentNotes>
                  <RoundIncidentCreatedByID>129</RoundIncidentCreatedByID>
                  <RoundIncidentCreatedDate>2019-04-08T16:12:26.493</RoundIncidentCreatedDate>
               </RoundIncidents>            
         </GetSiteIncidentsResult>
      </GetSiteIncidentsResponse>
   </soap:Body>
</soap:Envelope>
xml xslt xslt-1.0
1个回答
3
投票

删除孩子

将模板添加到identity transform,该模板匹配父元素和子元素具有相同QName的子元素,并且绕过子元素:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*[name() = name(..)]">
    <xsl:apply-templates select="node()"/>
  </xsl:template>

</xsl:stylesheet>

Demo关于Martin Honnen的方便的XSLT小提琴。

请注意,上面的词法比较QNames,其中可能包括名称空间前缀。要正确地比较名称(语义),请检查名称空间URI:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*[    local-name() = local-name(..) 
                         and namespace-uri() = namespace-uri(..)]">
    <xsl:apply-templates select="node()"/>
  </xsl:template>

</xsl:stylesheet>

更新了demo


删除父

根据OP的更新请求,这里是如何在有一个名为相同的子元素时删除父元素。在选择采用哪种方法时,请考虑一般情况下,孩子只能拥有一个父母,但父母可以拥有多个孩子。

简单的QName check

<xsl:template match="*[*[name() = name(..)]]">
  <xsl:apply-templates select="node()"/>
</xsl:template>

完整命名空间URI check

<xsl:template match="*[*[    local-name() = local-name(..) 
                         and namespace-uri() = namespace-uri(..)]]">
  <xsl:apply-templates select="node()"/>
</xsl:template>

信用:感谢@Alejandro@michael.hor257k提供有用的更正和改进。

© www.soinside.com 2019 - 2024. All rights reserved.