locked
Transformation of missing node in the input XML to a default value. RRS feed

  • Question

  • Currently am using a looping functoid to loop through <Identifier> and then use equal to functoid along with value mapping functoid to write out the <value>. But in case the node is missing in the input, the ouput node is doesnt get populated with 'NA' though the schema has a default value of 'NA'. Any thoughts?

    Input file(sample below): The color, shape and size are optional and may or may not be present in the input XML under the <Identifier> node. The final XML should have node for (COLOR, SHAPE and SIZE) and default it to 'NA', if the input is missing any of these nodes (COLOR, SHAPE or SIZE).

    I'm calling a SP to insert into a table and since the XML output generated is missing some nodes(COLOR,SHAPE,SIZE), the insert fails.


    Sample Input:

    <Product>
    <Identifier>
      <Key>COLOR</Key>
      <Value>RED</Value>
    </Identifier>
    <Identifier>
      <Key>SHAPE</Key>
      <Value>CIRCLE</Value>
    </Identifier>
    <Identifier>
      <Key>SIZE</Key>
      <Value>100</Value>
    </Identifier>
    </Product>

     

    Desired Output:
    <Product>
     <COLOR>RED</COLOR>
     <SHAPE>CIRCLE</SHAPE>
     <SIZE>100</SIZE>
    </Product>


    Raja
    • Edited by Sax001 Tuesday, April 19, 2011 8:52 PM addition
    Tuesday, April 19, 2011 8:34 PM

Answers

  • No worries.  In XSLT you could check for existience of a node like count(/product/identifier/key[. = 'COLOR') = 1 and if it is there map with value-of and if it is not map with your NA value (also using value of).  This can be used with xs:element to output what you want.  Make it an Inline xslt script and connect it where you need.

    -Dan


    If this answers your question, please Mark as Answer
    Tuesday, April 19, 2011 10:12 PM

All replies

  • Use a logical existence functiod [?] to see if the node exists, if it doesn't you can value map NA.  To do this connect the [?] to a [!] to the value mapping functiod.

    Kind Regards,

    -Dan


    If this answers your question, please Mark as Answer
    Tuesday, April 19, 2011 8:53 PM
  • I had tried this option too. if I dont have to loop through, it works fine.

    The logical existence will be true even if only one of the <Key> exist. I want to have 'NA' only for that specifc <key>(Color,Shape or Size) which is missing. 

    I also tried using '= functoid' and on returning false, --> [!] and then value mapping with 'NA'.  Since there is a loop, which loops through 3 times in this case, the output end ups with 'NA' three times for the missing <key> node.


    Raja
    Tuesday, April 19, 2011 9:35 PM
  • You need to also use the key name in your comparision map, a single exists isn't what you need, I'm just telling you how you can use exist to do this.  The real quick way to do this is with XSLT in a script functoid, but you can do it without, you'll just need to play around with it some.

    -Dan


    If this answers your question, please Mark as Answer
    Tuesday, April 19, 2011 9:40 PM
  • I'm using the key name in the comparison map. thats how it works fine when the <key> node exist. guess will have to play around and see, if I get lucky...:). Thnx you for reverting back quick.
    Raja
    Tuesday, April 19, 2011 9:44 PM
  • No worries.  In XSLT you could check for existience of a node like count(/product/identifier/key[. = 'COLOR') = 1 and if it is there map with value-of and if it is not map with your NA value (also using value of).  This can be used with xs:element to output what you want.  Make it an Inline xslt script and connect it where you need.

    -Dan


    If this answers your question, please Mark as Answer
    Tuesday, April 19, 2011 10:12 PM