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

mat*_*rns 10 xml xpath xsd jaxb xjc

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

 <events>
   <event name="CreatedNotification" />
   <event name="InfoLog" />
   <event name="UpdatedNotification" />
 </events>
Run Code Online (Sandbox Code Playgroud)

详细问题:

我想从xsd模式中选择多个complexTypes来与JAXB绑定.这适用于单个类:OrderStateChangeNotification

<jxb:bindings schemaLocation="apiv2.xsd">
  <jxb:bindings node="//xs:complexType[@name='OrderStateChangeNotification']">
    <inheritance:implements>com.google.checkout.sdk.notifications.Notification</inheritance:implements> 
  </jxb:bindings>
</jxb:bindings>
Run Code Online (Sandbox Code Playgroud)

以下是架构架构文件中的相关代码段:

  <xs:complexType name="OrderStateChangeNotification">
    <xs:all>
      <xs:element name="new-fulfillment-order-state" type="tns:FulfillmentOrderState" />
      <xs:element name="new-financial-order-state" type="tns:FinancialOrderState" />
      <xs:element name="previous-fulfillment-order-state" type="tns:FulfillmentOrderState" />
      <xs:element name="previous-financial-order-state" type="tns:FinancialOrderState" />
      <xs:element name="reason" type="xs:string" minOccurs="0" />
      <xs:element name="timestamp" type="xs:dateTime" />
      <xs:element name="google-order-number" type="xs:token" />
      <xs:element name="order-summary" type="tns:OrderSummary" minOccurs="0" />
    </xs:all>
    <xs:attribute name="serial-number" type="xs:string" use="required" />
  </xs:complexType>

  <xs:complexType name="ChargeAmountNotification">
    <xs:all>
      <xs:element name="timestamp" type="xs:dateTime" />
      <xs:element name="latest-charge-amount" type="tns:Money" />
      <xs:element name="latest-charge-fee" type="tns:FeeStructure" minOccurs="0" />
      <xs:element name="total-charge-amount" type="tns:Money" />
      <xs:element name="latest-promotion-charge-amount" type="tns:Money" minOccurs="0" />
      <xs:element name="google-order-number" type="xs:token" />
      <xs:element name="order-summary" type="tns:OrderSummary" minOccurs="0" />
    </xs:all>
    <xs:attribute name="serial-number" type="xs:string" use="required" />
  </xs:complexType>
Run Code Online (Sandbox Code Playgroud)

我希望绑定适用于所有通知对象.他们都以"通知"结束

我试过改变XPath

//xs:complexType[@name='OrderStateChangeNotification']
Run Code Online (Sandbox Code Playgroud)

//xs:complexType[substring(name(), string-length(name()) - 12) = 'Notification']
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

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

更新: @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
Run Code Online (Sandbox Code Playgroud)

我最后只是单独枚举它们.

Lee*_*eco 5

随着

//xs:complexType[substring(name(), string-length(name()) - 12) = 'Notification']
Run Code Online (Sandbox Code Playgroud)

表达式,您要求元素名称以"通知"结尾的所有元素.您真的想要询问具有以"通知"结尾的名称属性的所有元素.

试试这个:

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


bou*_*mbh 5

我有同样的问题.我发现multipleXJC使用了一个允许多节点匹配的属性.

我还希望绑定应用于每个模式位置.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>
Run Code Online (Sandbox Code Playgroud)

编辑:我在没有阅读问题评论的情况下发布了这个答案.对不起.我正在使用org.codehaus.mojo:jaxb2-maven-plugin:1.5带有XJC插件的maven插件,org.jvnet.jaxb2_commons:jaxb2-basics-project:0.6.4它似乎像这样工作......