选择具有XPath的节点以使用JAXB绑定类的子集 [英] Selecting nodes with XPath for binding a subset of classes with JAXB
问题描述
简化问题:
什么是选择所有XML节点的XPath,其属性以字符串Notification结尾。此代码段中的第一个和第三个节点:
Simplified Question: What's the XPath to select all XML nodes with an attribute that ends with the string "Notification". The first and third nodes in this snippet:
<events>
<event name="CreatedNotification" />
<event name="InfoLog" />
<event name="UpdatedNotification" />
</events>
详细问题:
我想从xsd模式中选择多个complexTypes来与JAXB绑定。这适用于单个类:OrderStateChangeNotification
I want to select multiple complexTypes from a xsd schema for binding with JAXB. This works for a single class: 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>
以下是架构架构文件中的相关代码段:
Here is the relevant snippet from the schema schema file:
<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>
我希望绑定适用于所有通知对象。它们都以通知结尾
I want the binding to apply to all notification objects. They all end in with "Notification"
我尝试从
//xs:complexType[@name='OrderStateChangeNotification']
to
//xs:complexType[substring(name(), string-length(name()) - 12) = 'Notification']
但它不起作用。
另一种方法是尝试选择具有子项order-summary和serial-number的所有节点,因为我知道只有Notification对象具有这些。
Another approach is to try and select all nodes with the children "order-summary" and "serial-number" as I know only Notification objects have these.
更新:
@Lee Greco的解决方案正确选择了我想要的节点,但不幸的是,继承插件与多个节点不兼容:
UPDATE: The solution by @Lee Greco correctly selectes the nodes I wanted, but unfortunatly, the inheritance plugin is not compatible with multiple nodes:
[ERROR] XPath evaluation of "//xs:complexType[substring(@name, string-length(@name)-string-length('Notification')+1)='Notification']" results in too many (8) target nodes
我最终只是单独列举它们。
I ended up just enumerating them separately.
推荐答案
使用
//xs:complexType[substring(name(), string-length(name()) - 12) = 'Notification']
表达式,您要求元素名称以通知结尾的所有元素。你真的想要求所有名称属性以'通知'结尾的元素。
expression you're asking for all elements where the element name ends in 'Notification'. You really want to ask for all elements with a name attribute that ends in 'Notification'.
请改为尝试:
//xs:complexType[substring(@name, string-length(@name)-string-length("Notification")+1)="Notification"]
这篇关于选择具有XPath的节点以使用JAXB绑定类的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!