我使用XMLSpy编写以下XPath来确定最长的字符串长度,它在XMLSpy中工作:
string-length(//exception:ElementMessageAbcException/@exceptionMsg[not(string-length(.) < //exception:ElementMessageAbcException/@exceptionMsg/string-length(.))] )
但是当我在spring集成中将相同的字符串放入xpath-expression时,它会出错:
org.springframework.beans.factory.BeanCreationException:错误 创建名为&x; xpathMaxLengthExceptionMsg&#39;的bean:实例化 豆子失败了;嵌套异常是 org.springframework.beans.factory.BeanDefinitionStoreException: 工厂方法[public static org.springframework.xml.xpath.XPathExpression org.springframework.xml.xpath.XPathExpressionFactory.createXPathExpression(java.lang.String中,java.util.Map) 投 java.lang.IllegalStateException,org.springframework.xml.xpath.XPathParseException] 抛出异常;嵌套异常是 org.springframework.xml.xpath.XPathParseException:无法编译 [//例外:MarkitMessageAciException / @ exceptionMsg [否(字符串长度() &LT; //例外:MarkitMessageAciException / @ exceptionMsg /串长度())]] 到XPathExpression:null;嵌套异常是 javax.xml.xpath.XPathExpressionException引起:javax.xml.transform.TransformerException:未知nodetype:string-length
<int-xml:xpath-expression id="xpathMaxLengthExceptionMsg" expression="//exception:ElementMessageAciException/@exceptionMsg[not(string-length(.) < //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>
答案 0 :(得分:0)
我不知道XML Spy是如何工作的,但我看到了这一点:
/@exceptionMsg/string-length(.)
语法错误。 string-length()
实际上是xPath function
,但它不是XML的节点。
目前尚不清楚你的目标是什么,但我希望它能够有所帮助。
<强>更新强>
好吧,我已经知道发生了什么。
您使用 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属性。