Simplified Question: 选择具有以字符串"Notification"结尾的属性的所有XML节点的XPath是什么 . 此代码段中的第一个和第三个节点:
<events>
<event name="CreatedNotification" />
<event name="InfoLog" />
<event name="UpdatedNotification" />
</events>
Detailed Question:
我想从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>
以下是架构架构文件中的相关代码段:
<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>
我希望绑定适用于所有通知对象 . 他们都以“通知”结束
我试过改变XPath
//xs:complexType[@name='OrderStateChangeNotification']
至
//xs:complexType[substring(name(), string-length(name()) - 12) = 'Notification']
但它不起作用 .
另一种方法是尝试选择具有子项“order-summary”和“serial-number”的所有节点,因为我知道只有Notification对象具有这些 .
UPDATE: @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
我最后只是单独枚举它们 .
3 回答
随着
表达式,您要求所有元素名称以'Notification'结尾的元素 . 您真的想要求name属性以'Notification'结尾的所有元素 .
试试这个:
我有同样的问题 . 我发现有一个
multiple
属性被XJC用来允许多个节点匹配 .我还希望绑定应用于每个模式位置 .
xs:anyURI
没有用,但我找到了使用*
令牌的方法 . 我添加了required="false"
属性以忽略不包含任何匹配的模式 .Edit: 我在没有阅读问题评论的情况下发布了这个答案 . 对不起 . 我正在使用maven插件
org.codehaus.mojo:jaxb2-maven-plugin:1.5
和XJC插件org.jvnet.jaxb2_commons:jaxb2-basics-project:0.6.4
,它似乎像这样工作......我最终得到一个类似的问题“太多的目标节点(3)”然而在任何网站上找不到任何答案...发布我经过大量的追踪和错误后发现的解决方案...解决的基本思路“太多目标节点(3)“是给出XSD中多个节点的完整XPATH .
以下是我的XSD:
以下是适用于上述XSD的JAXB绑定文件: