1

我正在尝试从 LabVIEW 架构创建一个 pyxb 模块。使用模块我得到一个 UnrecognizedDOMRootNodeError:

Traceback (most recent call last):
File "X:\Projects\LV-PY\Python\LV-PY\pyXB\testXML.py", line 14, in <module>
  fc_return = cluster.CreateFromDOM(doc.documentElement)
File "X:\Projects\LV-PY\Python\LV-PY\pyXB\cluster.py", line 76, in 
 CreateFromDOM
return pyxb.binding.basis.element.AnyCreateFromDOM(node, default_namespace)
File "C:\Python27\lib\site-packages\pyxb\binding\basis.py", line 1743, in 
 AnyCreateFromDOM
return cls.CreateDOMBinding(node, expanded_name.elementBinding(), 
    _fallback_namespace=fallback_namespace)
File "C:\Python27\lib\site-packages\pyxb\binding\basis.py", line 1705, in   
    CreateDOMBinding
raise pyxb.UnrecognizedDOMRootNodeError(node)
   pyxb.exceptions_.UnrecognizedDOMRootNodeError: <pyxb.utils.saxdom.Element
   object at 0x0625F9D0>

由于 LabVIEW 架构很大,我只用一个包含字符串的集群制作了一个小样本:

<xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:lv="http://www.ni.com/labview" elementFormDefault="qualified"
targetNamespace="http://www.ni.com/labview"
xmlns="http://www.ni.com/labview">

<!--Root element that contains all other data-->
<!--will NOT be produced by primitive-->
<xsd:element name="LVData" type="lv:LVDataRootType"/>
<xsd:complexType name="LVDataRootType">
  <xsd:choice minOccurs="0" maxOccurs="unbounded">
    <xsd:element name="Cluster" type="lv:ClusterType"/>
    <xsd:element name="String" type="lv:StringType"/>
  </xsd:choice>
</xsd:complexType>

<!--Basic elements-->

<xsd:complexType name="StringType">
  <xsd:complexContent>
    <xsd:extension base="lv:LVDataType"/>
   </xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="LVDataType">
    <xsd:sequence>
        <xsd:element name="Name" type="xsd:string" minOccurs="1"/>
        <xsd:element name="Val" type="xsd:string" minOccurs="1"/>
    </xsd:sequence>
</xsd:complexType>

<!--Cluster element-->
<xsd:complexType name="ClusterType">
  <xsd:sequence>  
      <xsd:element name="Name" type="xsd:string"/>
      <xsd:element name="NumElts" type="xsd:unsignedByte"/>
        <xsd:choice minOccurs="0" maxOccurs="unbounded">
            <xsd:element name="String" type="lv:StringType"/>
        </xsd:choice>
   </xsd:sequence>
</xsd:complexType>

</xsd:schema>

然后我从一个简单的 LabVIEW 集群发送 XML 输出:

<Cluster>
<Name>clTest</Name>
<NumElts>1</NumElts>
<String>
<Name>sTest</Name>
<Val>foo</Val>
</String>
</Cluster>

我猜这个问题与pabigot的回复有关:a similar question

在 XML Schema 中,元素和类型的命名空间是不同的,但在 Python 中却不是,

但我不太确定。我是否需要更改整个 LabView 架构中的lv:...Type以具有相同的名称和类型?

换句话说,而不是:

    <xsd:element name="Cluster" type="lv:ClusterType"/>

我应该有:

    <xsd:element name="Cluster" type="Cluster"/>

这将是对 LabVIEW 架构的重大更改,它始终执行此类操作。

4

1 回答 1

0

There are two problems with your document. The schema expects a top-level element {http://www.ni.com/labview}LVData, while your document has a top-level element Cluster. You need to wrap your document, and add a namespace declaration. The document below does not produce an error.

<LVData xmlns="http://www.ni.com/labview">
  <Cluster>
    <Name>clTest</Name>
    <NumElts>1</NumElts>
    <String>
      <Name>sTest</Name>
      <Val>foo</Val>
    </String>
  </Cluster>
</LVData>
于 2017-05-13T19:59:38.023 回答