Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 使用XPath选择节点以使用JAXB绑定类的子集_Xml_Xpath_Jaxb_Xsd_Xjc - Fatal编程技术网

Xml 使用XPath选择节点以使用JAXB绑定类的子集

Xml 使用XPath选择节点以使用JAXB绑定类的子集,xml,xpath,jaxb,xsd,xjc,Xml,Xpath,Jaxb,Xsd,Xjc,简化问题: 选择属性以字符串“Notification”结尾的所有XML节点的XPath是什么。此代码段中的第一个和第三个节点: <events> <event name="CreatedNotification" /> <event name="InfoLog" /> <event name="UpdatedNotification" /> </events> 到 但它不起作用 另一种方法是尝试选择所有具有子节

简化问题: 选择属性以字符串“Notification”结尾的所有XML节点的XPath是什么。此代码段中的第一个和第三个节点:

 <events>
   <event name="CreatedNotification" />
   <event name="InfoLog" />
   <event name="UpdatedNotification" />
 </events>

但它不起作用

另一种方法是尝试选择所有具有子节点“order summary”和“serial number”的节点,因为我知道只有通知对象具有这些节点

更新: @Lee Greco的解决方案正确地选择了我想要的节点,但不幸的是,继承插件与多个节点不兼容:

[ERROR] XPath evaluation of "//xs:complexType[substring(@name, string-length(@name)-string-length('Notification')+1)='Notification']" results in too many (8) target nodes
最后,我只是分别列举了它们

//xs:complexType[substring(name(), string-length(name()) - 12) = 'Notification']
表达式您要求元素名称以“通知”结尾的所有元素。您确实希望请求具有以“Notification”结尾的name属性的所有元素

请尝试以下方法:

//xs:complexType[substring(@name, string-length(@name)-string-length("Notification")+1)="Notification"]

我也有同样的问题。我发现XJC使用了一个
multiple
属性来允许多节点匹配

我还希望绑定应用于每个模式位置
xs:anyURI
不起作用,但我找到了一种使用
*
令牌的方法。我添加了
required=“false”
属性,以便忽略不包含任何匹配项的模式

<jxb:bindings schemaLocation="*">
  <jxb:bindings node="//xs:complexType[substring(name(), string-length(name()) - 12) = 'Notification']" multiple="true" required="false">
    <inheritance:implements>com.google.checkout.sdk.notifications.Notification</inheritance:implements> 
  </jxb:bindings>
</jxb:bindings>

com.google.checkout.sdk.notifications.Notification

编辑:我没有阅读问题的评论就发布了这个答案。对不起。我使用maven plugin
org.codehaus.mojo:jaxb2 maven plugin:1.5
和XJC plugin
org.jvnet.jaxb2_commons:jaxb2 basics项目:0.6.4
,它似乎是这样工作的…

我最终遇到了一个类似的问题“目标节点太多(3)”然而,在任何一个站点上都找不到任何答案…发布了我在经过大量跟踪和错误后发现的解决方案…解决“太多目标节点(3)”的基本思想是给出节点的完整XPATH,该节点在XSD中是多个的

下面是我的XSD:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="document">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="asset">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="attribute" maxOccurs="unbounded" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="string" minOccurs="0">
                      <xs:complexType>
                        <xs:simpleContent>
                          <xs:extension base="xs:string">
                            <xs:attribute type="xs:string" name="value" use="optional"/>
                          </xs:extension>
                        </xs:simpleContent>
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="date" minOccurs="0">
                      <xs:complexType>
                        <xs:simpleContent>
                          <xs:extension base="xs:string">
                            <xs:attribute type="xs:string" name="value" use="optional"/>
                          </xs:extension>
                        </xs:simpleContent>
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="array" minOccurs="0">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="struct"  maxOccurs="unbounded" minOccurs="0">
                            <xs:complexType>
                              <xs:sequence>
                                <xs:element name="field" maxOccurs="unbounded" minOccurs="0">
                                  <xs:complexType>
                                    <xs:sequence>
                                      <xs:element name="integer" minOccurs="0">
                                        <xs:complexType>
                                          <xs:simpleContent>
                                            <xs:extension base="xs:string">
                                              <xs:attribute type="xs:byte" name="value"/>
                                            </xs:extension>
                                          </xs:simpleContent>
                                        </xs:complexType>
                                      </xs:element>
                                      <xs:element name="assetreference" minOccurs="0">
                                        <xs:complexType>
                                          <xs:simpleContent>
                                            <xs:extension base="xs:string">
                                              <xs:attribute type="xs:string" name="type"/>
                                              <xs:attribute type="xs:long" name="value"/>
                                            </xs:extension>
                                          </xs:simpleContent>
                                        </xs:complexType>
                                      </xs:element>
                                    </xs:sequence>
                                    <xs:attribute type="xs:string" name="name" use="optional"/>
                                  </xs:complexType>
                                </xs:element>
                              </xs:sequence>
                            </xs:complexType>
                          </xs:element>
                          <xs:element name="integer" minOccurs="0">
                            <xs:complexType>
                              <xs:simpleContent>
                                <xs:extension base="xs:string">
                                  <xs:attribute type="xs:long" name="value"/>
                                </xs:extension>
                              </xs:simpleContent>
                            </xs:complexType>
                          </xs:element>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="file" minOccurs="0">
                      <xs:complexType>
                        <xs:simpleContent>
                          <xs:extension base="xs:string">
                            <xs:attribute type="xs:string" name="name" use="optional"/>
                          </xs:extension>
                        </xs:simpleContent>
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="integer" minOccurs="0">
                      <xs:complexType>
                        <xs:simpleContent>
                          <xs:extension base="xs:string">
                            <xs:attribute type="xs:short" name="value"/>
                          </xs:extension>
                        </xs:simpleContent>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                  <xs:attribute type="xs:string" name="name" use="optional"/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute type="xs:long" name="id"/>
            <xs:attribute type="xs:string" name="type"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

下面是为上述XSD工作的JAXB绑定文件:

<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
          xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          version="2.1">
    <bindings schemaLocation= "../assetproduct.xsd" version="1.0">
        <!-- Customise the package name 
        <schemaBindings>
            <package name="com.example.schema"/>
        </schemaBindings> -->

        <!-- rename the value element -->
        <bindings node="//xs:element[@name='document']">
            <bindings node="//xs:element[@name='asset']">
                <bindings node="//xs:element[@name='attribute']">

                    <bindings node="//xs:element[@name='string']">
                        <bindings node=".//xs:attribute[@name='value']">
                            <property name="ValueAttribute"/>
                        </bindings>
                    </bindings>


                    <bindings node="//xs:element[@name='date']">
                        <bindings node=".//xs:attribute[@name='value']">
                            <property name="ValueAttribute"/>
                        </bindings>
                    </bindings>

                    <bindings node="//xs:element[@name='array']">

                        <bindings node=".//xs:element[@name='struct']">
                            <bindings node=".//xs:element[@name='field']">

                                <bindings node=".//xs:element[@name='integer']/xs:complexType">
                                    <bindings node=".//xs:attribute[@name='value']">
                                        <property name="ValueAttribute"/>
                                    </bindings>
                                </bindings>

                                <bindings node=".//xs:element[@name='assetreference']">
                                    <bindings node=".//xs:attribute[@name='value']">
                                        <property name="ValueAttribute"/>
                                    </bindings>
                                </bindings>

                            </bindings>
                        </bindings>

                    </bindings>

                    <bindings node=".//xs:element[@name='array']/xs:complexType/xs:sequence/xs:element[@name='integer']">
                            <bindings node=".//xs:attribute[@name='value']">
                                <property name="ValueAttribute"/>
                            </bindings>
                    </bindings>

                    <bindings node="//xs:element[@name='attribute']/xs:complexType/xs:sequence/xs:element[@name='integer']">
                        <bindings node=".//xs:attribute[@name='value']">
                            <property name="ValueAttribute"/>
                        </bindings>
                    </bindings>
                </bindings>
            </bindings>
          </bindings>
    </bindings>
</bindings>

//xs:complexType[substring(@name, string-length(@name)-string-length("Notification")+1)="Notification"]
<jxb:bindings schemaLocation="*">
  <jxb:bindings node="//xs:complexType[substring(name(), string-length(name()) - 12) = 'Notification']" multiple="true" required="false">
    <inheritance:implements>com.google.checkout.sdk.notifications.Notification</inheritance:implements> 
  </jxb:bindings>
</jxb:bindings>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="document">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="asset">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="attribute" maxOccurs="unbounded" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="string" minOccurs="0">
                      <xs:complexType>
                        <xs:simpleContent>
                          <xs:extension base="xs:string">
                            <xs:attribute type="xs:string" name="value" use="optional"/>
                          </xs:extension>
                        </xs:simpleContent>
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="date" minOccurs="0">
                      <xs:complexType>
                        <xs:simpleContent>
                          <xs:extension base="xs:string">
                            <xs:attribute type="xs:string" name="value" use="optional"/>
                          </xs:extension>
                        </xs:simpleContent>
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="array" minOccurs="0">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="struct"  maxOccurs="unbounded" minOccurs="0">
                            <xs:complexType>
                              <xs:sequence>
                                <xs:element name="field" maxOccurs="unbounded" minOccurs="0">
                                  <xs:complexType>
                                    <xs:sequence>
                                      <xs:element name="integer" minOccurs="0">
                                        <xs:complexType>
                                          <xs:simpleContent>
                                            <xs:extension base="xs:string">
                                              <xs:attribute type="xs:byte" name="value"/>
                                            </xs:extension>
                                          </xs:simpleContent>
                                        </xs:complexType>
                                      </xs:element>
                                      <xs:element name="assetreference" minOccurs="0">
                                        <xs:complexType>
                                          <xs:simpleContent>
                                            <xs:extension base="xs:string">
                                              <xs:attribute type="xs:string" name="type"/>
                                              <xs:attribute type="xs:long" name="value"/>
                                            </xs:extension>
                                          </xs:simpleContent>
                                        </xs:complexType>
                                      </xs:element>
                                    </xs:sequence>
                                    <xs:attribute type="xs:string" name="name" use="optional"/>
                                  </xs:complexType>
                                </xs:element>
                              </xs:sequence>
                            </xs:complexType>
                          </xs:element>
                          <xs:element name="integer" minOccurs="0">
                            <xs:complexType>
                              <xs:simpleContent>
                                <xs:extension base="xs:string">
                                  <xs:attribute type="xs:long" name="value"/>
                                </xs:extension>
                              </xs:simpleContent>
                            </xs:complexType>
                          </xs:element>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="file" minOccurs="0">
                      <xs:complexType>
                        <xs:simpleContent>
                          <xs:extension base="xs:string">
                            <xs:attribute type="xs:string" name="name" use="optional"/>
                          </xs:extension>
                        </xs:simpleContent>
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="integer" minOccurs="0">
                      <xs:complexType>
                        <xs:simpleContent>
                          <xs:extension base="xs:string">
                            <xs:attribute type="xs:short" name="value"/>
                          </xs:extension>
                        </xs:simpleContent>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                  <xs:attribute type="xs:string" name="name" use="optional"/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute type="xs:long" name="id"/>
            <xs:attribute type="xs:string" name="type"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
          xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          version="2.1">
    <bindings schemaLocation= "../assetproduct.xsd" version="1.0">
        <!-- Customise the package name 
        <schemaBindings>
            <package name="com.example.schema"/>
        </schemaBindings> -->

        <!-- rename the value element -->
        <bindings node="//xs:element[@name='document']">
            <bindings node="//xs:element[@name='asset']">
                <bindings node="//xs:element[@name='attribute']">

                    <bindings node="//xs:element[@name='string']">
                        <bindings node=".//xs:attribute[@name='value']">
                            <property name="ValueAttribute"/>
                        </bindings>
                    </bindings>


                    <bindings node="//xs:element[@name='date']">
                        <bindings node=".//xs:attribute[@name='value']">
                            <property name="ValueAttribute"/>
                        </bindings>
                    </bindings>

                    <bindings node="//xs:element[@name='array']">

                        <bindings node=".//xs:element[@name='struct']">
                            <bindings node=".//xs:element[@name='field']">

                                <bindings node=".//xs:element[@name='integer']/xs:complexType">
                                    <bindings node=".//xs:attribute[@name='value']">
                                        <property name="ValueAttribute"/>
                                    </bindings>
                                </bindings>

                                <bindings node=".//xs:element[@name='assetreference']">
                                    <bindings node=".//xs:attribute[@name='value']">
                                        <property name="ValueAttribute"/>
                                    </bindings>
                                </bindings>

                            </bindings>
                        </bindings>

                    </bindings>

                    <bindings node=".//xs:element[@name='array']/xs:complexType/xs:sequence/xs:element[@name='integer']">
                            <bindings node=".//xs:attribute[@name='value']">
                                <property name="ValueAttribute"/>
                            </bindings>
                    </bindings>

                    <bindings node="//xs:element[@name='attribute']/xs:complexType/xs:sequence/xs:element[@name='integer']">
                        <bindings node=".//xs:attribute[@name='value']">
                            <property name="ValueAttribute"/>
                        </bindings>
                    </bindings>
                </bindings>
            </bindings>
          </bindings>
    </bindings>
</bindings>