2012-12-02 112 views
4

我正在使用Nokogiri來修改現有的XML,但我在選擇某些節點時遇到了問題。Nokogiri XPath找不到某些節點

這裏是XML的相關片段:

<ProductCatalog> 
    <ProductLineItem> 
    <updi:ProductIdentification> 
     <updi:ProductName>800-22283-03</updi:ProductName> 

我可以找到與較低的兩個節點:

doc.xpath("//updi:ProductIdentification") => #<Nokogiri::XML... 
doc.xpath("//updi:ProductName") => #<Nokogiri::XML... 

但是如果我儘量選擇上的一個節點:

doc.xpath("//ProductLineItem") => [] 

我找回一個空數組。這似乎與前綴有關。我可以找到任何有前綴的元素,但找不到沒有前綴的元素。

更新:這裏是(相當長)命名空間:

xsi:schemaLocation="urn:rosettanet:specification:interchange:ProductCatalogInformationDistribution:xsd:schema:01.00 ..\..\XML\Interchange\ProductCatalogInformationDistribution_01_00.xsd" 
xmlns:dplcs="urn:rosettanet:specification:domain:Design:ProductLifeCycleStatusCode:xsd:codelist:01.03" 
xmlns:rrt="urn:rosettanet:specification:domain:Shared:RateType:xsd:codelist:01.01" 
xmlns:dl="urn:rosettanet:specification:domain:Logistics:xsd:schema:02.15" 
xmlns:ictc="urn:rosettanet:specification:domain:Design:CatalogType:xsd:codelist:01.00" 
xmlns:updi="urn:rosettanet:specification:universal:ProductIdentification:xsd:schema:01.04" 
xmlns:dddt="urn:rosettanet:specification:domain:Design:DateType:xsd:codelist:01.00" 
xmlns:dsdc="urn:rosettanet:specification:domain:Logistics:ShipDateCode:xsd:codelist:01.03" 
xmlns:ucr="urn:rosettanet:specification:universal:Currency:xsd:codelist:01.02" 
xmlns:dpiac="urn:rosettanet:specification:domain:Logistics:PortIdentifierAuthorityCode:xsd:codelist:01.03" 
xmlns:rptc="urn:rosettanet:specification:domain:Shared:PricingTypeCode:xsd:codelist:01.03" 
xmlns:dit="urn:rosettanet:specification:domain:Procurement:InventoryType:xsd:codelist:01.03" 
xmlns:dtt="urn:rosettanet:specification:domain:Procurement:TransactionType:xsd:codelist:01.04" 
xmlns:upd="urn:rosettanet:specification:universal:PhysicalDimension:xsd:schema:01.05" 
xmlns:dcst="urn:rosettanet:specification:domain:Logistics:CustomsType:xsd:codelist:01.03" 
xmlns:dsd="urn:rosettanet:specification:domain:Logistics:ShippingDocument:xsd:codelist:01.02" 
xmlns:uci="urn:rosettanet:specification:universal:ContactInformation:xsd:schema:01.03" 
xmlns:dpcm="urn:rosettanet:specification:domain:Procurement:PurchaseMethod:xsd:codelist:01.03" 
xmlns:rpsc="urn:rosettanet:specification:domain:Shared:ProductStatusCode:xsd:codelist:01.01" 
xmlns:dgrc="urn:rosettanet:specification:domain:Marketing:GeographicRegionCode:xsd:codelist:01.02" 
xmlns:dtrt="urn:rosettanet:specification:domain:Logistics:TrackingReferenceType:xsd:codelist:01.06" 
xmlns:umtq="urn:rosettanet:specification:universal:MimeTypeQualifier:xsd:codelist:01.02" 
xmlns:dcrt="urn:rosettanet:specification:domain:Procurement:CustomerType:xsd:codelist:01.03" 
xmlns:dscd="urn:rosettanet:specification:domain:Logistics:ShipmentChangeDisposition:xsd:codelist:01.03" 
xmlns:uc="urn:rosettanet:specification:universal:Country:xsd:codelist:01.02" 
xmlns="urn:rosettanet:specification:interchange:ProductCatalogInformationDistribution:xsd:schema:01.00" 
xmlns:dpc="urn:rosettanet:specification:domain:Procurement:PaymentCondition:xsd:codelist:01.03" 
xmlns:rpmt="urn:rosettanet:specification:domain:Shared:PaymentType:xsd:codelist:01.01" 
xmlns:dft="urn:rosettanet:specification:domain:Procurement:FinanceTerms:xsd:codelist:01.03" 
xmlns:dtq="urn:rosettanet:specification:domain:Procurement:TotalQualifier:xsd:codelist:01.03" 
xmlns:ume="urn:rosettanet:specification:universal:MonetaryExpression:xsd:schema:01.04" 
xmlns:dcp="urn:rosettanet:specification:domain:Design:Compliant:xsd:codelist:01.02" 
xmlns:drsc="urn:rosettanet:specification:domain:Marketing:RegistrationStatusCode:xsd:codelist:01.03" 
xmlns:uat="urn:rosettanet:specification:universal:AbstractType:xsd:schema:01.02" 
xmlns:dp="urn:rosettanet:specification:domain:Procurement:xsd:schema:02.17" 
xmlns:rpm="urn:rosettanet:specification:domain:Shared:PaymentMethod:xsd:codelist:01.02" 
xmlns:dfrt="urn:rosettanet:specification:domain:Procurement:ForecastReferenceType:xsd:codelist:01.03" 
xmlns:dtec="urn:rosettanet:specification:domain:Procurement:TaxExemptionCode:xsd:codelist:01.03" 
xmlns:ulc="urn:rosettanet:specification:universal:Locations:xsd:schema:01.04" 
xmlns:dccc="urn:rosettanet:specification:domain:Procurement:CreditCardClassification:xsd:codelist:01.03" 
xmlns:drlc="urn:rosettanet:specification:domain:Logistics:ReturnLabelCode:xsd:codelist:01.03" 
xmlns:st="http://www.ascc.net/xml/schematron" 
xmlns:dnecc="urn:rosettanet:specification:domain:Logistics:NationalExportControlClassification:xsd:codelist:01.03" 
xmlns:rpktc="urn:rosettanet:specification:domain:Shared:PackageTypeCode:xsd:codelist:01.01" 
xmlns:uwt="urn:rosettanet:specification:universal:WeightType:xsd:codelist:01.01" 
xmlns:dfpt="urn:rosettanet:specification:domain:Logistics:FreightPaymentTerms:xsd:codelist:01.03" 
xmlns:dte="urn:rosettanet:specification:domain:Procurement:TransportEvent:xsd:codelist:01.03" 
xmlns:ul="urn:rosettanet:specification:universal:Language:xsd:codelist:01.02" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:dbpq="urn:rosettanet:specification:domain:Procurement:BookPriceQualifier:xsd:codelist:01.04" 
xmlns:drl="urn:rosettanet:specification:domain:Logistics:RouteLocation:xsd:codelist:01.03" 
xmlns:ssdh="urn:rosettanet:specification:system:StandardDocumentHeader:xsd:schema:01.16" 
xmlns:dmk="urn:rosettanet:specification:domain:Marketing:xsd:schema:02.12" 
xmlns:rmat="urn:rosettanet:specification:domain:Shared:MonetaryAmountType:xsd:codelist:01.01" 
xmlns:uuom="urn:rosettanet:specification:universal:UnitOfMeasure:xsd:codelist:01.03" 
xmlns:dfe="urn:rosettanet:specification:domain:Procurement:ForecastEvent:xsd:codelist:01.03" 
xmlns:dst="urn:rosettanet:specification:domain:Procurement:ShipmentTerms:xsd:codelist:01.03" 
xmlns:udt="urn:rosettanet:specification:universal:DataType:xsd:schema:01.04" 
xmlns:dacc="urn:rosettanet:specification:domain:Procurement:AccountClassification:xsd:codelist:01.03" 
xmlns:dptt="urn:rosettanet:specification:domain:Logistics:PortType:xsd:codelist:01.03" 
xmlns:sha="urn:rosettanet:specification:domain:Shared:xsd:schema:01.10" 
xmlns:dlv="urn:rosettanet:specification:domain:Design:Level:xsd:codelist:01.02" 
xmlns:rict="urn:rosettanet:specification:domain:Shared:InvoiceChargeType:xsd:codelist:01.02" 
xmlns:utt="urn:rosettanet:specification:universal:TaxType:xsd:codelist:01.02" 
xmlns:ddwsr="urn:rosettanet:specification:domain:Marketing:DesignWinStatusReason:xsd:codelist:01.03" 
xmlns:dsm="urn:rosettanet:specification:domain:Logistics:ShipmentMode:xsd:codelist:01.05" 
xmlns:udct="urn:rosettanet:specification:universal:DocumentType:xsd:codelist:01.09" 
xmlns:dac="urn:rosettanet:specification:domain:Design:ActionCode:xsd:codelist:01.03" 
xmlns:dpsr="urn:rosettanet:specification:domain:Procurement:ProductSubstitutionReason:xsd:codelist:01.03" 
xmlns:sft="urn:rosettanet:specification:system:TPIRFileType:xsd:codelist:01.01" 
xmlns:dltcc="urn:rosettanet:specification:domain:Procurement:LeadTimeClassificationCode:xsd:codelist:01.03" 
xmlns:ri="urn:rosettanet:specification:domain:Shared:Interval:xsd:codelist:01.01" 
xmlns:urss="urn:rosettanet:specification:system:xml:1.0" 
xmlns:dds="urn:rosettanet:specification:domain:Design:xsd:schema:02.15" 
xmlns:dslt="urn:rosettanet:specification:domain:Procurement:SaleType:xsd:codelist:01.04" 
xmlns:udc="urn:rosettanet:specification:universal:Document:xsd:schema:01.08" 
xmlns:dabcc="urn:rosettanet:specification:domain:Design:ABCCode:xsd:codelist:01.02" 
xmlns:dppt="urn:rosettanet:specification:domain:Procurement:ProductProcurementType:xsd:codelist:01.03" 
xmlns:rwtc="urn:rosettanet:specification:domain:Shared:WarrantyType:xsd:codelist:01.01" 
xmlns:dlit="urn:rosettanet:specification:domain:Logistics:InstructionType:xsd:codelist:01.00" 
xmlns:rfob="urn:rosettanet:specification:domain:Shared:FreeOnBoard:xsd:codelist:01.01" 
xmlns:upri="urn:rosettanet:specification:universal:ProcessRoleIdentifier:xsd:codelist:01.08" 
xmlns:ddrn="urn:rosettanet:specification:domain:Marketing:DesignRegistrationNotification:xsd:codelist:01.02" 
xmlns:dsh="urn:rosettanet:specification:domain:Procurement:SpecialHandling:xsd:codelist:01.04" 
xmlns:ud="urn:rosettanet:specification:universal:Dates:xsd:schema:01.03" 
xmlns:dpms="urn:rosettanet:specification:domain:Marketing:ProjectMarketSegment:xsd:codelist:01.02" 
xmlns:rssl="urn:rosettanet:specification:domain:Shared:ShippingServiceLevel:xsd:codelist:01.01" 
xmlns:dldr="urn:rosettanet:specification:domain:Logistics:LotDiscrepancyReason:xsd:codelist:01.03" 
xmlns:rat="urn:rosettanet:specification:domain:Shared:AmountType:xsd:codelist:01.02" 
xmlns:upi="urn:rosettanet:specification:universal:PartnerIdentification:xsd:schema:01.12" 
xmlns:ddp="urn:rosettanet:specification:domain:Marketing:Disposition:xsd:codelist:01.02" 
xmlns:dsfr="urn:rosettanet:specification:domain:Procurement:SpecialFulfillmentRequest:xsd:codelist:01.03" 
xmlns:ucs="urn:rosettanet:specification:universal:CountrySubdivision:xsd:codelist:01.02 
+0

什麼是文檔根目錄下的命名空間? – Phrogz

+0

這是相當長的....我會將它添加到我的問題 – Pynner

+0

請參閱下面的答案;您的更新不是文檔的命名空間。您正在尋找根元素上的'xmlns =「...」'屬性。 – Phrogz

回答

8

最簡單快速黑客的解決方案是完全從文檔中刪除命名空間:

require 'nokogiri' 
xml = Nokogiri.XML "<root xmlns='foo' xmlns:bar='whee'><a/><bar:b /></root>" 

p xml.xpath('//b').length  #=> 0 
p xml.xpath('//bar:b').length #=> 1 
p xml.xpath('//a').length  #=> 0 
xml.remove_namespaces! 
p xml.xpath('//a').length  #=> 1 
p xml.xpath('//b').length  #=> 1 

然而,上述不一個有效的解決方案,如果你需要保留名字空間(例如修改你的文檔並保存它,或者你在各種名字空間中有相互衝突的元素或屬性名稱)。如果你不能核彈的命名空間,你可以創建一個前綴,並告訴引入nokogiri它所對應...

xml = Nokogiri.XML "<root xmlns='foo' xmlns:bar='whee'><a/><bar:b /></root>" 
p xml.xpath('//x:a','x'=>'foo').length #=> 1 

...其中字符串foo是URI爲擁有元素的文檔中的命名空間有一個默認的命名空間(通常在根目錄下)和字符串x就是你想要的(不與已經在你的文檔中聲明的另一個命名空間相沖突)。或者更簡單地說,你可以只使用xmlns作爲默認命名空間的前綴:

p xml.xpath('//xmlns:a').length #=> 1 

另外,如果你需要離開的命名空間,並可以構建一個合理的CSS樣式選擇,以獲得您所需要那麼節點您可以使用css方法:

require 'nokogiri' 
xml = Nokogiri.XML "<root xmlns='foo' xmlns:bar='whee'> 
    <a/> 
    <bar:b /> 
    <c xmlns='jim'><d/></c> 
</root>" 

p xml.css('a').length, #=> 1 
    xml.css('b').length, #=> 0 
    xml.css('c').length, #=> 0 
    xml.css('d').length #=> 0 

如上圖所示,注意,這僅適用於那些在相同的命名空間的根元素節點。

+0

我對命名空間的理解是非常簡單的。我真的不明白爲什麼這個作品....但它確實!謝謝。 – Pynner

+0

@Pynner每個XML元素和屬性都可以與一個名稱空間相關聯。命名空間只是一個用來唯一標識它的URI。但是,每次需要SVG圓元素時,編寫''是不可能的,因此有兩種更簡單的方式來分配一個名稱空間。 1)您可以通過您構成的簡寫標識符前綴來引用一個名稱空間;例如' ... '在該命名空間中創建一個'jim'元素。 2)元素的默認命名空間('xmlns')被所有非前綴的後代繼承。 – Phrogz

+0

感謝您的解釋,同樣也適用於其他人從事相同類型的問題。你可以使用'doc來引用默認的命名空間。xpath('// x:element','x'=> doc.namespaces ['xmlns'])' – Pynner