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

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

问题描述

 <?xml version =1.0encoding =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>/v9:Source> ;<v9:Code>0000</ v9:消息>成功< / v9:消息>< v9:LocalizedMessage>成功< / 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优先级和LT; / 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> PricingCode>实际< / V9:PricingCode>< V9:RatedWeightMethod>实际< / V9:RatedWeightMethod>< V9:CurrencyExchangeRate>< V9:FromCurrency> EUR< / V9:FromCurrency>< V9:IntoCurrency>美元< / v9:DimDivisor> 0< / v9:DimDivisor>< v9:FuelSurchargePercent> 12.5< / v9:IntoCurrency>< v9:Rate> 1.37< / v9:Rate>< / v9:CurrencyExchangeRate> FuelSurchargePercent>

这是我的Web服务的SOAP响应。



我要 XmlSearch()获取< v9:image> 内容。



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

解决方案

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

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

在这里,您忽略v9的XML声明,并在XPath搜索中明确引用命名空间 namespace-uri(),并使用 local-name()函数请求元素的值。 p>

如果在XML文档的根目录(最上面)声明了v9,那么使用// v9:Image /尝试的旧XPath搜索语法将会起作用。 / p>

由于ColdFusion XPath库实现,在执行搜索时,没有办法任意设置命名空间上下文;您仅限于在文档根目录中声明的命名空间(并且它们被转换为XPath可以搜索的默认命名空间),因此允许您使用命名空间别名(如// v9:Image /)执行搜索。



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


<?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>

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

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

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

解决方案

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']") />

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.

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.

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.

SOURCE: Specify xmlns namespaces in an XPath Search

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

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