我有困难将一个SOAP响应XML转换为纯文本字符串。我开始使用XLST并阅读了所有相关资料。显然,我需要完成的任务很简单,但是所有示例都比我的情境简单得多。
首先,我正在访问Bing Maps Reverse Geocoding的Web服务,该服务返回以下XML结构:
节点b:GeocodeResult重复了约10次。其他带有
首先,我正在访问Bing Maps Reverse Geocoding的Web服务,该服务返回以下XML结构:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<ReverseGeocodeResponse xmlns="http://dev.virtualearth.net/webservices/v1/geocode/contracts">
<ReverseGeocodeResult xmlns:a="http://dev.virtualearth.net/webservices/v1/geocode" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<BrandLogoUri xmlns="http://dev.virtualearth.net/webservices/v1/common">
http://dev.virtualearth.net/Branding/logo_powered_by.png
</BrandLogoUri>
<ResponseSummary xmlns="http://dev.virtualearth.net/webservices/v1/common">
<AuthenticationResultCode>ValidCredentials</AuthenticationResultCode>
<Copyright>(...)</Copyright>
<FaultReason i:nil="true" />
<StatusCode>Success</StatusCode>
<TraceId>(...)</TraceId>
</ResponseSummary>
<a:Results xmlns:b="http://dev.virtualearth.net/webservices/v1/common">
<b:GeocodeResult>
<b:Address>
<b:AddressLine>(...)</b:AddressLine>
<b:AdminDistrict>SP</b:AdminDistrict>
<b:CountryRegion>Brasil</b:CountryRegion>
<b:District />
<b:FormattedAddress>(...)</b:FormattedAddress>
<b:Locality>Campinas</b:Locality>
<b:PostalCode>13069-380</b:PostalCode>
<b:PostalTown />
</b:Address>
<b:BestView>(...)</b:BestView>
<b:Confidence>Medium</b:Confidence>
<b:DisplayName>(...)</b:DisplayName>
<b:EntityType>Address</b:EntityType>
<b:Locations>(...)</b:Locations>
<b:MatchCodes xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<c:string>Good</c:string>
</b:MatchCodes>
</b:GeocodeResult>
<b:GeocodeResult>
(...)
</b:GeocodeResult>
</a:Results>
</ReverseGeocodeResult>
</ReverseGeocodeResponse>
</s:Body>
</s:Envelope>
节点b:GeocodeResult重复了约10次。其他带有
(...)
的部分是无关的(没有相关的节点)。
我需要从这个详尽的响应中获得的是节点b:Locality和b:AdminDistrict。
我已经努力了几天,但还是没有完成。
以下是其中的一种方法:<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://dev.virtualearth.net/webservices/v1/common"
xmlns:a="http://dev.virtualearth.net/webservices/v1/geocode"
xmlns:b="http://dev.virtualearth.net/webservices/v1/common"
xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<xsl:template match="/s:Envelope/s:Body/ReverseGeocodeResponse/ReverseGeocodeResult/a:Results/b:GeocodeResult/b:Address">
<xsl:value-of select="b:Locality"/> - <xsl:value-of select="b:AdminDistrict"/>
</xsl:template>
</xsl:stylesheet>
我知道这应该只返回第一个 b:Locality
和 b:AdminDistrict
节点,这很完美。但是当我尝试这样做时,结果是XML中的所有文本(没有标签,只有连接在一起的文本)。这种方法的一些变体仅返回两个 xsl:value-of
标签之间的 ' - ' 片段。
我做错了什么?这可能与无限的命名空间有关吗?