tXMLMap not supporting some XPath expressions?

One Star

tXMLMap not supporting some XPath expressions?

Hi,
In a tXMLMap component I try to get node having specific attribute value.
So I use a XPath like:
myNode

The XPath is valid, but I always get NULL in the results!?
Is there a limitation or bug in using [] in tXMLMap expressions? 

Here are details of an example job:   
0. Job screenshots 

1. Input XML / schema:
<catalog catalog-id="test-catalog">
   <product product-id="TEST-bs-variant-01">
       <custom-attributes>
           <custom-attribute attribute-id="color">red</custom-attribute>
       </custom-attributes>
   </product>
   <product product-id="TEST-bs-variant-02">
       <custom-attributes>
           <custom-attribute attribute-id="alternativeColor">navy</custom-attribute>
           <custom-attribute attribute-id="color">blue</custom-attribute>
       </custom-attributes>
   </product>
</catalog>

2. Used expression in tXmlMap:
] 

3. Result
         ACTUAL                             EXPECTED
.------------------+-----.          .------------------+-----.
|       tLogRow_1        |          |       tLogRow_1        |
|=-----------------+----=|          |=-----------------+----=|
|productID         |color|          |productID         |color|
|=-----------------+----=|          |=-----------------+----=|
|TEST-bs-variant-01|null |          |TEST-bs-variant-01|red  |
|TEST-bs-variant-02|null |          |TEST-bs-variant-02|blue |
'------------------+-----'          '------------------+-----'

Thanks,
Mirko
Sixteen Stars

Re: tXMLMap not supporting some XPath expressions?

You are making this more complicated than it needs be. You do not need to use an XPath query for this. Your available attributes on your row1 dataset are prefixed with @. Just drag them to the other side. If you really want to use XPath queries to extract your data, maybe try using the tExtractXMLField component (https://help.talend.com/search/all?query=tExtractXMLField&content-lang=en). This is what I use to deal with more complicated XML structures.
One Star

Re: tXMLMap not supporting some XPath expressions?

You are making this more complicated than it needs be. You do not need to use an XPath query for this. Your available attributes on your row1 dataset are prefixed with @. Just drag them to the other side. If you really want to use XPath queries to extract your data, maybe try using the tExtractXMLField component (). This is what I use to deal with more complicated XML structures.

Thank you, but I think this is not relevant to my question and given example. 
(The given job is just an example to illustrate the problem. In reality I have a lot more complex and huge structure to parse, not a single field which could be done in other ways like tExtractXMLField)
Sixteen Stars

Re: tXMLMap not supporting some XPath expressions?

tXMLMap components do not require XPath queries. You should have an XML schema as your input, output or both. You simply join the dots between your input and out (attributes included). If you want to filter data according to the value of your elements or attributes, you either use the tXMLMap variables (the box between the input and output) or use the output filter (click the white arrow with a green plus symbol in it). 
The advantage of the tXMLMap is that it allows very easy access to your XML data, but at a cost of only truly supporting comparatively simple tasks. For more complex interactions with XML you can either use the tExtractXMLField (you can use this for more than one field and can use it to retrieve complex types as node sets, etc), use the Talend Mapper (if you have the Enterprise Edition) or make use of any Java API you choose to interact with your XML.  
One Star

Re: tXMLMap not supporting some XPath expressions?

So, if I understand you correctly, tXMLMap does not support XPath expressions, but only simple node referencing?
Other than that I don't see how variables can help in this case.
Other options (including later aggregation / filtring) seem very heavy. 
So I'll go with custom tJavaRow if that's the case.
Thanks,
Mirko
Sixteen Stars

Re: tXMLMap not supporting some XPath expressions?

You have to understand that XML is a hierarchical data structure which can be loosely equated to a small set of related RDBMS tables. As such, you can get multiple flattened rows from a single file. You might want to restrict the "rows" that get to the output using the variables or the output filter that I mentioned previously.
But if you simply want to get the value of an element based on its attribute value, this can be done by using the attribute and the element with a tXMLMap variable. 
I sense that you are probably a Java developer that is new to Talend. If this is the case, at first Talend can seem a bit counter intuitive. However, there is method in what first appears to be madness. The biggest mistake made by people who know Java and are new to Talend is "recreating the wheel". Using Talend to trigger bespoke Java code. There is nothing wrong with writing your own code and making use of third party APIs (that is one of the things that makes Talend great in my eyes), just be careful not to "recreate the wheel".
Good luck.
One Star

Re: tXMLMap not supporting some XPath expressions?

Hi,
It needs to be understood that exactly because XML is hierarchical, it's important to be able to describe some advanced mapping so that you can transform it to the proper flat structure (filtering does not help in most cases - you'll need another join - not supported by Talend, or using aggregate functions - in cases like this very heavy). 
But if you simply want to get the value of an element based on its attribute value, this can be done by using the attribute and the element with a tXMLMap variable. 

In case you indeed have an idea, could you please write the exact variable expression that can be used to achieve the goal from example?

As you suggest I was trying not to "reinvent the wheel" and lost much time trying to make the native components work... just to understand that Talend does not properly support even some basic operations. Apart from the mentioned xpath in tXMLMap expressions these are things like:
- Limited tFileInputXML when using SAX parser:
xpath expressions (as the one from example above) also don't work, nevertheless the GUI component suggests they should (I know XPath works on DOM, but the loop element could be, if not already, loaded as DOM, so it's probably just due to its implementation). 
- Self joins:
if you have multiple outputs from tMap or even tReplicate components they can not be used as multiple inputs in another Map component so you can not make self join (a workaround could be using temporary storage and read it from there, but it's additional complication...)
I could create examples illustrating the problems, but only if there is any point (i.e. someone will look at them and take them into account). Else generic posts that Talend is perfect and we should use/buy it, may sound pleasant, but do not help much to overcome the problems and make it actually great ;-).
Best Regards,
Mirko