简体   繁体   English

如何在骆驼中找到Xpath并为标签添加值

[英]How to find a Xpath and add value to the tags in Camel

I'm trying to find an Xpath from exchange body and add a value to the tag. 我试图从交换正文中找到一个Xpath并将一个值添加到标签中。

<root>
<details1>    
   <Name>Ying</Name>
   <status></status>
</details1>
<details2>    
   <Name>Ying</Name>
   <status></status>
</details2>
</root>

I want to find xPath=root/details2/status From this exchange body and add value to the status. 我想从此交换主体中找到xPath=root/details2/status并为状态添加值。 Since there are two occurances of status in the body String I will not be able to use String1.replace('<status></status>',<status>no</status>) Is there any way to use camel Xpath to find the correct tag and add the value? 由于字符串主体中有两种状态出现,所以我将无法使用String1.replace('<status></status>',<status>no</status>)有没有办法使用骆驼Xpath来找到正确的标签并添加值?

With xpath you can find and read the correct tag. 使用xpath,您可以找到并读取正确的标签。 But how are you going to write it? 但是你要怎么写呢? One solution could be to use the xpath inside an xlst tranformation. 一种解决方案是在xlst转换中使用xpath。 So you can add the value you want to put in the element in a camel header "myHeader" and then use it in the xslt as an xsl:param . 因此,您可以将要放入元素的值添加到骆驼头“ myHeader”中,然后在xslt中将其用作xsl:param。

Assuming the xml is in the inbox folder then 假设xml在收件箱文件夹中,则

 from("file:inbox?noop=true")
 .setHeader("myHeader",constant("no"))
 .to("xslt:mytransform.xslt")
 .to("file:outbox/?fileName=out.xml");

Will put the value "no" in root/details2/status. 将值“ no”放在root / details2 / status中。

Where mytransform.xlst inside your /src/main/resources folder is like / 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[. Xpath / root / details2 / status [。 = ''] matches empty status elements ='']匹配空状态元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM