ColdFusion XmlSearch():带有命名空间的 XPath [英] ColdFusion XmlSearch(): XPath with namespaces

查看:21
本文介绍了ColdFusion XmlSearch():带有命名空间的 XPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><env:Header xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/><soapenv:Body><v9:ProcessShipmentReply xmlns:v9="http://fedex.com/ws/ship/v9"><v9:HighestSeverity xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">SUCCESS</v9:HighestSeverity><v9:Notifications xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><v9:Severity>SUCCESS</v9:Severity><v9:Source>ship</v9:Source><v9:Code>0000</v9:Code><v9:Message>Success</v9:Message><v9:LocalizedMessage>Success</v9:LocalizedMessage></v9:Notifications><v9:Version>
                            <v9:ServiceId>ship</v9:ServiceId>
                            <v9:Major>9</v9:Major>

                            <v9:Intermediate>0</v9:Intermediate>
                            <v9:Minor>0</v9:Minor>
                         </v9:Version><v9:CompletedShipmentDetail><v9:UsDomestic xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">false</v9:UsDomestic><v9:CarrierCode xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">FDXE</v9:CarrierCode><v9:ServiceTypeDescription xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">INTL PRIORITY</v9:ServiceTypeDescription><v9:PackagingDescription xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">YOUR_PACKAGING</v9:PackagingDescription><v9:RoutingDetail xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><v9:UrsaPrefixCode>9F</v9:UrsaPrefixCode><v9:UrsaSuffixCode>QNJA </v9:UrsaSuffixCode><v9:OriginLocationId>QVAA </v9:OriginLocationId><v9:OriginServiceArea>AM</v9:OriginServiceArea><v9:DestinationLocationId>QNJA </v9:DestinationLocationId><v9:DestinationServiceArea>PM</v9:DestinationServiceArea><v9:DestinationLocationStateOrProvinceCode>  </v9:DestinationLocationStateOrProvinceCode><v9:AstraPlannedServiceLevel>PM</v9:AstraPlannedServiceLevel><v9:AstraDescription>INTL PRIORITY</v9:AstraDescription><v9:PostalCode>74940</v9:PostalCode><v9:StateOrProvinceCode>  </v9:StateOrProvinceCode><v9:CountryCode>FR</v9:CountryCode><v9:AirportId>CDG</v9:AirportId></v9:RoutingDetail><v9:ShipmentRating xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><v9:ActualRateType>PAYOR_ACCOUNT_SHIPMENT</v9:ActualRateType><v9:ShipmentRateDetails><v9:RateType>PAYOR_ACCOUNT_SHIPMENT</v9:RateType><v9:RateScale>0000000</v9:RateScale><v9:RateZone>IT001O</v9:RateZone><v9:PricingCode>ACTUAL</v9:PricingCode><v9:RatedWeightMethod>ACTUAL</v9:RatedWeightMethod><v9:CurrencyExchangeRate><v9:FromCurrency>EUR</v9:FromCurrency><v9:IntoCurrency>USD</v9:IntoCurrency><v9:Rate>1.37</v9:Rate></v9:CurrencyExchangeRate><v9:DimDivisor>0</v9:DimDivisor><v9:FuelSurchargePercent>12.5</v9:FuelSurchargePercent><v9:TotalBillingWeight><v9:Units>KG</v9:Units><v9:Value>6.0</v9:Value></v9:TotalBillingWeight><v9:TotalBaseCharge><v9:Currency>USD</v9:Currency><v9:Amount>142.27</v9:Amount></v9:TotalBaseCharge><v9:TotalFreightDiscounts><v9:Currency>USD</v9:Currency><v9:Amount>0.0</v9:Amount></v9:TotalFreightDiscounts><v9:TotalNetFreight><v9:Currency>USD</v9:Currency><v9:Amount>142.27</v9:Amount></v9:TotalNetFreight><v9:TotalSurcharges><v9:Currency>USD</v9:Currency><v9:Amount>17.78</v9:Amount></v9:TotalSurcharges><v9:TotalNetFedExCharge><v9:Currency>USD</v9:Currency><v9:Amount>160.05</v9:Amount></v9:TotalNetFedExCharge><v9:TotalTaxes><v9:Currency>USD</v9:Currency><v9:Amount>0.0</v9:Amount></v9:TotalTaxes><v9:TotalNetCharge><v9:Currency>USD</v9:Currency><v9:Amount>160.05</v9:Amount></v9:TotalNetCharge><v9:TotalRebates><v9:Currency>USD</v9:Currency><v9:Amount>0.0</v9:Amount></v9:TotalRebates><v9:Surcharges><v9:SurchargeType>FUEL</v9:SurchargeType><v9:Description>Fuel</v9:Description><v9:Amount><v9:Currency>USD</v9:Currency><v9:Amount>17.78</v9:Amount></v9:Amount></v9:Surcharges></v9:ShipmentRateDetails><v9:ShipmentRateDetails><v9:RateType>RATED_ACCOUNT_SHIPMENT</v9:RateType><v9:RateScale>0000000</v9:RateScale><v9:RateZone>IT001O</v9:RateZone><v9:PricingCode>ACTUAL</v9:PricingCode><v9:RatedWeightMethod>ACTUAL</v9:RatedWeightMethod><v9:CurrencyExchangeRate><v9:FromCurrency>EUR</v9:FromCurrency><v9:IntoCurrency>EUR</v9:IntoCurrency><v9:Rate>1.0</v9:Rate></v9:CurrencyExchangeRate><v9:DimDivisor>0</v9:DimDivisor><v9:FuelSurchargePercent>12.5</v9:FuelSurchargePercent><v9:TotalBillingWeight><v9:Units>KG</v9:Units><v9:Value>6.0</v9:Value></v9:TotalBillingWeight><v9:TotalBaseCharge><v9:Currency>EUR</v9:Currency><v9:Amount>103.6</v9:Amount></v9:TotalBaseCharge><v9:TotalFreightDiscounts><v9:Currency>EUR</v9:Currency><v9:Amount>0.0</v9:Amount></v9:TotalFreightDiscounts><v9:TotalNetFreight><v9:Currency>EUR</v9:Currency><v9:Amount>103.6</v9:Amount></v9:TotalNetFreight><v9:TotalSurcharges><v9:Currency>EUR</v9:Currency><v9:Amount>12.95</v9:Amount></v9:TotalSurcharges><v9:TotalNetFedExCharge><v9:Currency>EUR</v9:Currency><v9:Amount>116.55</v9:Amount></v9:TotalNetFedExCharge><v9:TotalTaxes><v9:Currency>EUR</v9:Currency><v9:Amount>0.0</v9:Amount></v9:TotalTaxes><v9:TotalNetCharge><v9:Currency>EUR</v9:Currency><v9:Amount>116.55</v9:Amount></v9:TotalNetCharge><v9:TotalRebates><v9:Currency>EUR</v9:Currency><v9:Amount>0.0</v9:Amount></v9:TotalRebates><v9:Surcharges><v9:SurchargeType>FUEL</v9:SurchargeType><v9:Description>Fuel</v9:Description><v9:Amount><v9:Currency>EUR</v9:Currency><v9:Amount>12.95</v9:Amount></v9:Amount></v9:Surcharges></v9:ShipmentRateDetails></v9:ShipmentRating><v9:IneligibleForMoneyBackGuarantee xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">false</v9:IneligibleForMoneyBackGuarantee><v9:CompletedPackageDetails xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><v9:SequenceNumber>1</v9:SequenceNumber><v9:TrackingIds><v9:TrackingIdType>EXPRESS</v9:TrackingIdType><v9:FormId>0430</v9:FormId><v9:TrackingNumber>794794712260</v9:TrackingNumber></v9:TrackingIds><v9:GroupNumber>0</v9:GroupNumber><v9:Barcodes><v9:BinaryBarcodes><v9:Type>COMMON_2D</v9:Type><v9:Value>Wyk+HjAxHTAyNzQ5NDAdMjUwHTAxHTAwMDAwMDAwMDAwMDA0MzAdRkRFHTUxMDA4NzAyMB0wMjUdHTEvMR02LjBLRx1OHUxlIFBlcmNlIE5laWdlLCA0IENoZW1pbiBkZSBWZXJib3V4HUFubmVjeSBMZSBWaWV1eB1aWh1HcmFuZG91bGllciBNYXJpZS1MYXVyZR4wNh0xMFpFSUkwNh0xMVpTb2NpZXTDoCBEZXN0aW5hdGFyaW8dMTJaMDY3MTIzNzAyMR0zMVo0MDQ5NzE5OTExMDAwMDA0MDAwMDAwMDAwMDAwNDMwMB0zMlodMTRaKipURVNUIExBQkVMIC0gRE8gTk9UIFNISVAqKh0xNVoxMDAwOTE2NDQdMjhaHUtWYWx1ZSB0byBiZSBTaG93biBpbnRvIHRoZSBpbnYdOTlaRUkwMDA0HElUHDMzLjAcRVVSHENvbnRlbnQgZGVzY3JpcHRpb24gKE11c3QgYmUgcHJvdmlkZWQgaW4gRU5HTElTHBxZHDEwLjAcHTI2WjRmZmMcHR4E</v9:Value></v9:BinaryBarcodes><v9:StringBarcodes><v9:Type>ASTRA</v9:Type><v9:Value>40497199110000047947947122604300</v9:Value></v9:StringBarcodes></v9:Barcodes><v9:AstraLabelElements><v9:Number>2</v9:Number><v9:Content>TRK#</v9:Content></v9:AstraLabelElements><v9:AstraLabelElements><v9:Number>3</v9:Number><v9:Content>0430</v9:Content></v9:AstraLabelElements><v9:AstraLabelElements><v9:Number>5</v9:Number><v9:Content>9F QNJA </v9:Content></v9:AstraLabelElements><v9:AstraLabelElements><v9:Number>7</v9:Number><v9:Content>40497199110000047947947122604300</v9:Content></v9:AstraLabelElements><v9:AstraLabelElements><v9:Number>8</v9:Number><v9:Content>512G2/A78E/A278</v9:Content></v9:AstraLabelElements><v9:AstraLabelElements><v9:Number>10</v9:Number><v9:Content>7947 9471 2260</v9:Content></v9:AstraLabelElements><v9:AstraLabelElements><v9:Number>12</v9:Number><v9:Content>PM</v9:Content></v9:AstraLabelElements><v9:AstraLabelElements><v9:Number>13</v9:Number><v9:Content>INTL PRIORITY</v9:Content></v9:AstraLabelElements><v9:AstraLabelElements><v9:Number>15</v9:Number><v9:Content>74940</v9:Content></v9:AstraLabelElements><v9:AstraLabelElements><v9:Number>16</v9:Number><v9:Content>  -FR</v9:Content></v9:AstraLabelElements><v9:AstraLabelElements><v9:Number>17</v9:Number><v9:Content>CDG</v9:Content></v9:AstraLabelElements><v9:Label><v9:Type>OUTBOUND_LABEL</v9:Type><v9:ShippingDocumentDisposition>RETURNED</v9:ShippingDocumentDisposition><v9:Resolution>200</v9:Resolution><v9:CopiesToPrint>1</v9:CopiesToPrint><v9:Parts><v9:DocumentPartSequenceNumber>1</v9:DocumentPartSequenceNumber><v9:Image>HUGE STUFF!!!</v9:Image></v9:Parts></v9:Label><v9:SignatureOption>SERVICE_DEFAULT</v9:SignatureOption></v9:CompletedPackageDetails></v9:CompletedShipmentDetail></v9:ProcessShipmentReply></soapenv:Body></soapenv:Envelope>

这是我的 SOAP 响应,来自我的 Web 服务.

Here's my SOAP response, from my web service.

我想 XmlSearch() 来获取 <v9:image> 内容.

I want to XmlSearch() to get the <v9:image> content.

什么是正确的 XPath?//v9:image"和//image"都不起作用!

What's the right XPath? "//v9:image" nor "//image" do not work!

推荐答案

命名空间 v9 不在根目录,因此您必须稍微修改语法才能访问 XML 文档的 v9 部分中的元素:

The namespace v9 isn't at the root, so you'll have to modify your syntax slightly to access elements in the v9 section of the XML doc:

<cfset results = xmlSearch(myXMLDoc,"//*[local-name()='Image' and namespace-uri()='http://fedex.com/ws/ship/v9']") />

这里忽略v9的XML声明,通过namespace-uri()显式引用XPath搜索中的命名空间,并用local请求元素的值-name() 函数.

Here, you ignore the XML declaration of v9, and explicitly refer to the namespace in the XPath search via namespace-uri(), and request the value of the element with the local-name() function.

如果在 XML 文档的根(顶部)声明了 v9,那么您尝试使用的旧 XPath 搜索语法//v9:Image/将可以工作.

If v9 was declared at the root (up at the top) of the XML document, the old XPath search syntax you tried using //v9:Image/ would have worked.

由于 ColdFusion XPath 库的实现,无法在执行搜索时任意设置命名空间上下文;您仅限于在文档根部声明的名称空间(并且它们被注入 XPath 可以搜索的默认名称空间),因此允许您使用名称空间别名(例如//v9:Image/)执行搜索.随着您对 XML 的深入了解,引入了新的命名空间时,这种情况就会中断.

Due to the ColdFusion XPath library implementation, there's no way to set the namespace context arbitrarily while you perform your search; you're limited to the namespaces that are declared at the root of the document (and they are poured into the default namespace that XPath can search), thus allowing you to execute a search using namespace aliases such as //v9:Image/. This breaks when, as you get deeper into the XML, new namespaces are introduced.

来源:在 XPath 搜索中指定 xmlns 命名空间

这篇关于ColdFusion XmlSearch():带有命名空间的 XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆