【问题标题】:Querying xml: Undefined namespace prefix查询 xml:未定义的命名空间前缀
【发布时间】:2018-01-12 05:18:52
【问题描述】:

我在 Postgresql 9.4 中遇到问题,使用命名空间查询 xml 字段。

我正在使用以下内容:

select unnest(xpath('/cfdi:Comprobante/cfdi:Receptor/text()',  
       xmlfield,  
       array[array['cfdi:','http://www.sat.gob.mx/cfd/3']])) AS "receptor"  
       from mytable;  

我的 xml 字段看起来像

<cfdi:Comprobante  
  Version="3.3" Serie="W" Folio="713" 
  Fecha="2018-01-09T20:18:41" FormaPago="99" 
  NoCertificado="0022" CondicionesDePago="CONTADO" 
  SubTotal="5450.53" Moneda="MXN" 
  Total="6322.61" TipoDeComprobante="I" 
  MetodoPago="PUE" LugarExpedicion="55525" 
  Sello="N4MxzjrIqV/GOgUOw=="  
  xsi:schemaLocation="http://www.sat.gob.mx/cfd/3 
  http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv33.xsd" 
  Certificado="MIIsK2A==" 
  xmlns:cfdi="http://www.sat.gob.mx/cfd/3" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
<cfdi:Emisor 
  Rfc="MIO180526WW9" 
  Nombre="MI EMPRESA" 
  RegimenFiscal="601"   />
<cfdi:Receptor 
  Rfc="TUYO191111SOY" 
  Nombre="OSCAR TUNEZ YUNEZ" 
  UsoCFDI="P01"   />
<cfdi:Conceptos>
 <cfdi:Concepto 
   ClaveProdServ="151" Cantidad="599.299526" Descripcion="CHIVA FLACA" 
   Importe="5450.53" ClaveUnidad="LTR" Unidad="LITRO"
   ValorUnitario="9.094828">
  <cfdi:Impuestos>
   <cfdi:Traslados>
    <cfdi:Traslado 
      Base="5450.53"
      Impuesto="002"
      TipoFactor="Tasa"
      TasaOCuota="0.16"
      Importe="872.08"   />
   </cfdi:Traslados>
  </cfdi:Impuestos> 
</cfdi:Concepto>
</cfdi:Conceptos>
<cfdi:Impuestos TotalImpuestosTrasladados="872.08">
 <cfdi:Traslados>
  <cfdi:Traslado 
   Impuesto="002"
   TipoFactor="Tasa"
   TasaOCuota="0.16" 
   hImporte="872.08" />
 </cfdi:Traslados>
</cfdi:Impuestos>
<cfdi:Complemento>
 <tfd:TimbreFiscalDigital 
   Version="1.1" UUID="138" 
   FechaTimbrado="2019-07-09T20:18:48" 
   RfcProvCertif="MET310111MX9" 
   SelloCFD="fQVVPfUOw==" 
   SelloSAT="6PHUGnorpHQ=="
   NoCertificadoSAT="007" 
   xsi:schemaLocation="http://www.sat.gob.mx/TimbreFiscalDigital 
   http://www.sat.gob.mx/sitio_internet/cfd/timbrefl/TimbreFlv11.xsd"
   xmlns:tfd="http://www.sat.gob.mx/TimbreFiscalDigital" />
  </cfdi:Complemento>
</cfdi:Comprobante

我正在努力 cfdi:Comprobante cfdi:受体 Rfc="TUYO191111SOY" Nombre="OSCAR TUNEZ YUNEZ"

但是使用之前的查询返回

ERROR:  could not create XPath object
DETAIL:  Undefined namespace prefix
********** Error **********

ERROR: could not create XPath object
SQL state: XX000
Detail: Undefined namespace prefix

我做错了什么? 提前致谢

【问题讨论】:

    标签: xml postgresql


    【解决方案1】:

    您需要从 nsarray 中删除冒号。

    这将起作用:

    select unnest(xpath('/cfdi:Comprobante/cfdi:Receptor/text()',  
           xmlfield,  
           array[array['cfdi','http://www.sat.gob.mx/cfd/3']])) AS "receptor"  
           from mytable;
    

    但是会返回空结果,因为该元素下没有文本值。如果你想获得整个元素,那么就做/cfdi:Comprobante/cfdi:Receptor,或者如果你想要某些属性,那么使用@就像这样/cfdi:Comprobante/cfdi:Receptor/@Nombre

    【讨论】:

    • 感谢@LukasKaminski,它成功了,有没有办法将完整的属性集作为列?
    • @glicuado 如果您使用/elementName/@*,您可以获得元素下的所有属性,但它不返回属性名称,只返回值。我认为您要么必须知道要查找的属性并为每个属性执行单独的 xpath,要么获取所有属性并确保它们的顺序和数量在文档之间保持一致,或者在 XML 内容上使用正则表达式。
    猜你喜欢
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多