NIFI:从xml获取数据(评估Xpath)?

时间:2017-09-20 07:44:34

标签: xml xpath apache-nifi

我有类似的xml响应:

<?xml version="1.0" encoding="utf-8"?>
<DayInfo>
<TalkMessage 
        xmlns="***">
        <EnvelopeVersion>**</EnvelopeVersion>
        <Header>
            <MessageDetails>
                <Class>****</Class>
                <Qualifier>*****</Qualifier>
                <Function>***</Function>
                <CorrelationID>*****</CorrelationID>
                <ResponseEndPoint />
            </MessageDetails>
            <SenderDetails>
                <IDAuthentication />
                <EmailAddress />
            </SenderDetails>
        </Header>
        <TalkDetails></TalkDetails>

         <Body>
         with message like
         <person><../person>
         <contact>...</contact>
         <data>... </data>

         </Body>
         </DayInfo>

在我的evaluateXpath处理器中,我想提取数据,以便我得到3个不同的xml文件和三个不同的消息数据,我使用了这样的表达式: // * [local-name()='person'] < / strong>对于每条消息,我只得到人员数据的xml响应,但我也需要在我的xml中使用这些数据:

<TalkMessage 
        xmlns="***">
        <EnvelopeVersion>**</EnvelopeVersion>
        <Header>
            <MessageDetails>
                <Class>****</Class>
                <Qualifier>*****</Qualifier>
                <Function>***</Function>
                <CorrelationID>*****</CorrelationID>
                <ResponseEndPoint />
            </MessageDetails>
            <SenderDetails>
                <IDAuthentication />
                <EmailAddress />
            </SenderDetails>
        </Header>
        <TalkDetails></TalkDetails>

我应该改变什么以获得适当的响应(我的意思是使用TalkMessage数据的人员消息数据)? 人物信息的好反应示例是:

<?xml version="1.0" encoding="utf-8"?>
<DayInfo>
<TalkMessage 
        xmlns="***">
        <EnvelopeVersion>**</EnvelopeVersion>
        <Header>
            <MessageDetails>
                <Class>****</Class>
                <Qualifier>*****</Qualifier>
                <Function>***</Function>
                <CorrelationID>*****</CorrelationID>
                <ResponseEndPoint />
            </MessageDetails>
            <SenderDetails>
                <IDAuthentication />
                <EmailAddress />
            </SenderDetails>
        </Header>
        <TalkDetails></TalkDetails>

         <Body>

         <person>
         <id></id>
         <name></name>
         </person>


         </Body>
         </DayInfo>

2 个答案:

答案 0 :(得分:0)

如果您有机会使用xsl转换,我建议使用以下转换

<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:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="Body">
  <xsl:copy>
   <xsl:apply-templates select="person"/>
  </xsl:copy>
 </xsl:template>

</xsl:stylesheet>

答案 1 :(得分:0)

假设Body元素的唯一子元素是:

       
  •    
  • 接触
  •    
  • 数据

而不是xpath:

//*[local-name()='person']

尝试选择除联系人和数据元素之外的所有所需元素:

//*[local-name()!='contact' and local-name()!='data']