java-具有节点功能的Spring int-xml:xpath-expression错误

我使用XMLSpy编写以下XPath来确定最长的字符串长度,并且它可以在XMLSpy中使用:

string-length(//exception:ElementMessageAbcException/@exceptionMsg[not(string-length(.) < //exception:ElementMessageAbcException/@exceptionMsg/string-length(.))] )

但是,当我在春季集成中将相同的字符串放入xpath-expression中时,会出错:

org.springframework.beans.factory.BeanCreationException: Error
creating bean with name ‘xpathMaxLengthExceptionMsg’: Instantiation of
bean Failed; nested exception is
org.springframework.beans.factory.BeanDeFinitionStoreException:
Factory method [public static
org.springframework.xml.xpath.XPathExpression
org.springframework.xml.xpath.XPathExpressionFactory.createXPathExpression(java.lang.String,java.util.Map)
throws
java.lang.IllegalStateException,org.springframework.xml.xpath.XPathParseException]
threw exception; nested exception is
org.springframework.xml.xpath.XPathParseException: Could not compile
[//exception:MarkitMessageAciException/@exceptionMsg[not(string-length()
<
//exception:MarkitMessageAciException/@exceptionMsg/string-length())]]
to a XPathExpression: null; nested exception is
javax.xml.xpath.XPathExpressionException Caused by: javax.xml.transform.TransformerException: UnkNown nodetype: string-length

<int-xml:xpath-expression id="xpathMaxLengthExceptionMsg" expression="//exception:ElementMessageAciException/@exceptionMsg[not(string-length(.) &lt; //exception:ElementMessageAciException/@exceptionMsg/string-length(.))]">
    <map>
        <entry key="pricing" value="http://www.example.net/imdwrd/schemas/Element-pricing-aci" />
        <entry key="security" value="http://www.example.net/imdwrd/schemas/Element-security-aci" />
        <entry key="tns" value="http://www.example.net/imdwrd/schemas/Element-message-aci" />
        <entry key="internalClassification" value="http://www.example.net/imdwrd/schemas/Element-internal-classification" />
        <entry key="organization" value="http://www.example.net/imdwrd/schemas/Element-organization-aci" />
        <entry key="exception" value="http://www.example.net/imdwrd/schemas/Element-message-aci-exception"/>
        <entry key="xsi" value="http://www.w3.org/2001/XMLSchema-instance" />
    </map>
</int-xml:xpath-expression>

样本XML:

<?xml version="1.0" encoding="UTF-8"?>
<confirm:ElementMessageAbcConfirm
  confirmTs="2014-10-14T11:43:36.191-05:00"
  correlationId="ORG-1008891250014" numberAccepted="0"
  numberRejected="1" status="exception"
  xmlns:ack="http://www.example.net/imdwrd/schemas/Element-message-Abc-ack"
  xmlns:confirm="http://www.example.net/imdwrd/schemas/Element-message-Abc-confirm"
  xmlns:event="http://www.example.net/imdwrd/schemas/data-source-event"
  xmlns:exception="http://www.example.net/imdwrd/schemas/Element-message-Abc-exception"
  xmlns:internalClassification="http://www.example.net/imdwrd/schemas/Element-internal-classification"
  xmlns:ns9="http://www.example.net/imdwrd/schemas/Element-message-Abc"
  xmlns:organization="http://www.example.net/imdwrd/schemas/Element-organization-Abc"
  xmlns:pricing="http://www.example.net/imdwrd/schemas/Element-pricing-Abc" xmlns:security="http://www.example.net/imdwrd/schemas/Element-security-Abc">
  <exception:ElementMessageAbcException exceptionCode="1000"
    exceptionMsg="[property]=Organization [message]=Organization information has not changed.  Update request has been cancelled."
    exceptionType="informational" id="385059141722030" type="organization"/>
 <exception:ElementMessageAbcException exceptionCode="1000"
    exceptionMsg="[property]=Organization [message]=Organizatiosn information has not changed.  Update request has been cancelled. longer"
    exceptionType="informational" id="385059141722030" type="organization"/>    
</confirm:ElementMessageAbcConfirm>

解决方法:

我不知道XML Spy是如何工作的,但是我看到了这一点:

/@exceptionMsg/string-length(.)

错误的语法. string-length()实际上是xPath函数,但它不是XML的节点.

目前尚不清楚您的目标是什么,但我希望它能对您有所帮助.

UPDATE

好吧,我已经知道发生了什么事.

您使用的是XPath 2(How do I select an XML node with the longest child #text node value with XPath?),但是Java认情况下不具备该功能.

因此,您应该使用其他一些库. Saxon是此事上最好的.

但是,您应该这样做:

-Djavax.xml.xpath.XPathFactory:http://java.sun.com/jaxp/xpath/dom=net.sf.saxon.xpath.XPathFactoryImpl

或以编程方式指定该System属性.

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念