![](/img/trans.png)
[英]With camel, how do I use the value of a header to define an XPath expression?
[英]How to find a Xpath and add value to the tags in Camel
我试图从交换正文中找到一个Xpath并将一个值添加到标签中。
<root>
<details1>
<Name>Ying</Name>
<status></status>
</details1>
<details2>
<Name>Ying</Name>
<status></status>
</details2>
</root>
我想从此交换主体中找到xPath=root/details2/status
并为状态添加值。 由于字符串主体中有两种状态出现,所以我将无法使用String1.replace('<status></status>',<status>no</status>)
有没有办法使用骆驼Xpath来找到正确的标签并添加值?
使用xpath,您可以找到并读取正确的标签。 但是你要怎么写呢? 一种解决方案是在xlst转换中使用xpath。 因此,您可以将要放入元素的值添加到骆驼头“ myHeader”中,然后在xslt中将其用作xsl:param。
假设xml在收件箱文件夹中,则
from("file:inbox?noop=true")
.setHeader("myHeader",constant("no"))
.to("xslt:mytransform.xslt")
.to("file:outbox/?fileName=out.xml");
将值“ no”放在root / details2 / status中。
/ src / main / resources文件夹中的mytransform.xlst就像
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="myHeader"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/root/details2/status[. = '']">
<status><xsl:value-of select="$myHeader"/></status>
</xsl:template>
</xsl:stylesheet>
Xpath / root / details2 / status [。 ='']匹配空状态元素
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.