我得到的 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>
答案 0 :(得分: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小提琴。
请注意,以上按词法比较QName,其中可能包含名称空间前缀。要正确地比较名称( ),请检查名称空间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所做的更正和改进。