我得到的XML响应在响应中具有相同的命名元素,导致我出现问题,我需要使用XSLT 1.0删除此重复元素。响应中的元素是<RoundIncidents>
。我已经查看了其他问题,但无法找到将响应转换为正确格式所需的内容。
<?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>
<?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>
将模板添加到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提供有用的更正和改进。