Created on 06-15-2021 06:36 PM - edited 06-15-2021 11:37 PM
Hi,
is there some way that nifi could remove the attributes inside the XML tag name? Please advise with some example to help me. Appreciate. Thanks
Example:
<particular>
<name>Nicholas</name>
<height>183</height>
<weight>75</weight>
</particular>
<particular>
<name>Debbie</name>
<height163</height>
<weight>45</weight>
</particular>
to something like:
<particular>
<name>Nicholas</name>
<height unit = "meter">183</height>
<weight unit = "kilogram" unit = "lb">75</weight>
</particular>
<particular>
<name>Debbie</name>
<height unit = "meter">163</height>
<weight unit = "kilogram" unit = "lb">45</weight>
</particular>
Created 06-17-2021 06:50 AM
@techNerd
Based on yoru example, you can actually modify your XML using the ReplaceText [1] processor.
The processor can be configured with Java regex that would match on the string you want to remove and the replacement value would be set to an empty string.
MattWho_0-1623937282966.png
Your regex pattern in this case would need to match on (don't forget the spaces):
( unit = ".*?")The parenthesis mark the java regex capture group.
.*? is a non greedy match on any character for zero or more characters until the very next quote. This allows this to match on any variety of unit strings ( meter, kilogram, and lb) from your example
This will result in the following:
<particular>
<name>Nicholas</name>
<height unit = "meter">183</height>
<weight unit = "kilogram" unit = "lb">75</weight>
</particular>
<particular>
<name>Debbie</name>
<height unit = "meter">163</height>
<weight unit = "kilogram" unit = "lb">45</weight>
</particular>being converted to:
<particular>
<name>Nicholas</name>
<height>183</height>
<weight>75</weight>
</particular>
<particular>
<name>Debbie</name>
<height>163</height>
<weight>45</weight>
</particular>
If you found this addressed your query, please take a moment to login and click "Accept" on this solution.
Thank you,
Matt
Created 06-16-2021 06:26 AM
@techNerd
I am looking at your example and am not clear on what your are "removing"?
looks like you are adding text to your example.
Thanks,
Matt
Created 06-16-2021 07:09 PM
Hi @MattWho,
Apologies as I made a mistake on the example. The example should be as follow:
Input:
<particular>
<name>Nicholas</name>
<height unit = "meter">183</height>
<weight unit = "kilogram" unit = "lb">75</weight>
</particular>
<particular>
<name>Debbie</name>
<height unit = "meter">163</height>
<weight unit = "kilogram" unit = "lb">45</weight>
</particular>
Output:
<particular>
<name>Nicholas</name>
<height>183</height>
<weight>75</weight>
</particular>
<particular>
<name>Debbie</name>
<height>163</height>
<weight>45</weight>
</particular>
Are there some ways that NiFi processor could remove the attributes (unit or etc..) inside the XML tag name as mention in the example above? It will be a great help if there is an example with internal processor configuration shown and explanation.
Appreciate your help. Thanks.
Created 06-17-2021 06:50 AM
@techNerd
Based on yoru example, you can actually modify your XML using the ReplaceText [1] processor.
The processor can be configured with Java regex that would match on the string you want to remove and the replacement value would be set to an empty string.
MattWho_0-1623937282966.png
Your regex pattern in this case would need to match on (don't forget the spaces):
( unit = ".*?")The parenthesis mark the java regex capture group.
.*? is a non greedy match on any character for zero or more characters until the very next quote. This allows this to match on any variety of unit strings ( meter, kilogram, and lb) from your example
This will result in the following:
<particular>
<name>Nicholas</name>
<height unit = "meter">183</height>
<weight unit = "kilogram" unit = "lb">75</weight>
</particular>
<particular>
<name>Debbie</name>
<height unit = "meter">163</height>
<weight unit = "kilogram" unit = "lb">45</weight>
</particular>being converted to:
<particular>
<name>Nicholas</name>
<height>183</height>
<weight>75</weight>
</particular>
<particular>
<name>Debbie</name>
<height>163</height>
<weight>45</weight>
</particular>
If you found this addressed your query, please take a moment to login and click "Accept" on this solution.
Thank you,
Matt
Created 06-17-2021 08:06 PM
Created 07-29-2021 06:13 AM
Maybe a TransformXml with this XSLT might be more future proof:
<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 />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>