Support Questions
Find answers, ask questions, and share your expertise

NIFI:getting data from xml (Evaluate Xpath)?

Highlighted

NIFI:getting data from xml (Evaluate Xpath)?

I have xml response similar to this :

<?xml version="1.0" encoding="utf-8"?><DayInfo><TalkMessagexmlns="***"><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>

and in my evaluateXpath processors i want to extract data so that i got 3 different xml file with three different message data i used expressions like this ://*[local-name()='person'] for each messages and i got xml response with only person data but i need this data in my xml too:

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

what should i change to get appropriate response(i mean person message data with TalkMessage data)? good reponse example for person message is:

<?xml version="1.0" encoding="utf-8"?><DayInfo><TalkMessagexmlns="***"><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>
1 REPLY 1

Re: NIFI:getting data from xml (Evaluate Xpath)?

New Contributor

You won't be able to do what you want with a single XPath, but this XSLT...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
  xmlns:foo="***" exclude-result-prefixes="foo">
  <xsl:template match="/DayInfo">
    <xsl:for-each select="foo:TalkMessage/foo:Body/foo:person">
      <xsl:result-document method="xml" href="person_{foo:id}.xml">
        <DayInfo>
          <TalkMessage xmlns="***">
            <xsl:copy-of select="/DayInfo/foo:TalkMessage/foo:EnvelopeVersion" />
            <xsl:copy-of select="/DayInfo/foo:TalkMessage/foo:Header" />
            <xsl:copy-of select="/DayInfo/foo:TalkMessage/foo:TalkDetails" />
            <xsl:element name="Body" namespace="***">
              <xsl:copy-of select="." />
            </xsl:element>
          </TalkMessage>
        </DayInfo>
      </xsl:result-document>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

will convert this XML (which is similar to your sample XML response)...

<?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>
        <id>34</id>
        <name>Moira Theriault</name>
      </person>
      <person>
        <id>35</id>
        <name>Cheree Meacham</name>
      </person>
      <person>
        <id>36</id>
        <name>Kris Demery</name>
      </person>
      <contact>...</contact>
      <data>...</data>
    </Body>
  </TalkMessage>
</DayInfo>

and will create these three files, person_34.xml, person_35.xml, person_36.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>
      <person>
        <id>34</id>
        <name>Moira Theriault</name>
      </person>
    </Body>
  </TalkMessage>
</DayInfo>

<?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>35</id>
        <name>Cheree Meacham</name>
      </person>
    </Body>
  </TalkMessage>
</DayInfo>

<?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>36</id>
        <name>Kris Demery</name>
      </person>
    </Body>
  </TalkMessage>
</DayInfo>

Of course, you'll need to include your actual namespace.

If you don't have access to an XSLT 2.0 processor, you can use the <exslt:document> extension element to XSLT 1.0.