Skip to Content

Converting FF to Deep xml using XSLT

Hi Experts,

I am trying to convert flat file to a deeped xml strucuture in XSLT.

Could you please help me here in the nested loop case.

My Source xml is :

<?xml version=”1.0″ encoding=”UTF-8″?>
<AB>
<Record>
<A>
<RecordType/>
</A>
<B>
<RecordType>a</RecordType>
</B>
<C>
<RecordType>1</RecordType>
</C>
<B>
<RecordType>b</RecordType>
</B>
<C>
<RecordType>2</RecordType>
</C>
</Record>
</AB>

The output should be as below:

<?xml version=”1.0″ encoding=”UTF-8″?>
<AB>
<A>
<RecordType/>
</A>
<B>
<RecordType>a</RecordType>
<C>
<RecordType>1</RecordType>
</C>
</B>

<B>
<RecordType>b</RecordType>
<C>
<RecordType>2</RecordType>
</C>
</B>
</AB>

B and C segments are repeated.Under each B node respective C node should get repeated in target.

When i am trying to write a inner loop for C node under B in xslt. All the C nodes are getting repeated for each B.

Could you please suggest in XLST to achieve desired output

Thanks,

Shameer

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

5 Answers

  • Jan 28, 2017 at 04:32 PM

    Hi Shamir!

    From your example above I can't see any link between each B and C elements, only relative position - element C after element B. You can try following-sibling::C[1] xPath expression to get the very first C element staying after B element but you should keep in mind that it will stop working if elements order in message is changed.

    Regards, Evgeniy.

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 29, 2017 at 03:41 AM

    Hi Evgeniy!

    You are correct there is no link between B nodes and C nodes.As you said

    following-sibling::C[1] will get the first C element. But in my case B and C occurrences are 0..unbounded and order gets changed.The count of B and C nodes will differ.

    Example: In Source we may get one B node and three C nodes. The output should be all C nodes should go under B as mentioned below

    Source xml:

    <AB>
    <Record>
    <A>
    <RecordType/>
    </A>
    <B>
    <RecordType>a</RecordType>
    </B>
    <C>
    <RecordType>1</RecordType>
    </C>

    <C>
    <RecordType>2</RecordType>
    </C>

    <C>
    <RecordType>3</RecordType>
    </C>

    </Record>
    </AB>

    Output:

    <AB>
    <Record>
    <A>
    <RecordType/>
    </A>
    <B>
    <RecordType>a</RecordType>
    <C>
    <RecordType>1</RecordType>
    </C>

    <C>
    <RecordType>2</RecordType>
    </C>

    <C>
    <RecordType>3</RecordType>
    </C>

    </B>

    </Record>
    </AB>

    Many thanks Evgeniy for your suggestions and inputs.

    Br,

    Shameer

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 29, 2017 at 12:07 PM

    Hi Shameer!

    Does it mean that your output should contain all C nodes under each B node? Or please clarify your requirement further.

    Regards, Evgeniy.

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 29, 2017 at 04:54 PM

    Hi Evgeniy,

    Yes true. In output All C nodes will go under B.

    Add comment
    10|10000 characters needed characters exceeded

    • I mean that under each B node you want to place all C nodes from document. But you said before that you got it in inner loop and that was wrong.

      So, please clarify how do you want C nodes to be placed under B nodes.

      Regards, Evgeniy.

  • Feb 03, 2017 at 11:36 AM

    Hi Evgeniy,

    We had a little change in requirement after discussion. Let me make requirement clear.

    Record...1.Unbounded

    RR..0..1

    L..0..Unbounded

    T..0.Unbounded

    Source Xml:

    <Record>
    <RR>
    <RecordType>R</RecordType>
    </RR>
    <L>
    <RecordType>L</RecordType>
    <LoadNumber>1</LoadNumber>
    </L>
    <T>
    <RecordType>T</RecordType>
    <SAPOrderNumber>123</<SAPOrderNumber>
    </T>
    </Record>

    <Record>
    <L>
    <RecordType>L</RecordType>
    <LoadNumber>2</LoadNumber>
    </L>
    <T>
    <RecordType>T</RecordType>
    <SAPOrderNumber>456</<SAPOrderNumber>
    </T>
    <T>
    <RecordType>T</RecordType>
    <SAPOrderNumber>789</<SAPOrderNumber>
    </T>
    </Record>

    <Record>
    <L>
    <RecordType>L</RecordType>
    <LoadNumber>3</LoadNumber>

    </L>
    <T>
    <RecordType>T</RecordType>
    <SAPOrderNumber>ABC</<SAPOrderNumber>
    </T>
    <T>
    <RecordType>T</RecordType>
    <SAPOrderNumber>DEF</<SAPOrderNumber>
    </T>
    <L>
    <RecordType>L</RecordType>
    <LoadNumber>4</LoadNumber>
    </L>
    <T>
    <RecordType>T</RecordType>
    <SAPOrderNumber>GHI</<SAPOrderNumber>
    </T>
    </Record>

    Target xml:
    <RR>
    <RecordType>R</RecordType>
    </RR>
    <L>
    <RecordType>L</RecordType>
    <LoadNumber>1</LoadNumber>
    <T>
    <RecordType>T</RecordType>
    <SAPOrderNumber>123</<SAPOrderNumber>
    </T>
    </L>
    <L>
    <RecordType>L</RecordType>
    <LoadNumber>2</LoadNumber>
    <T>
    <RecordType>T</RecordType>
    <SAPOrderNumber>456</<SAPOrderNumber>
    </T>
    <T>
    <RecordType>T</RecordType>
    <SAPOrderNumber>789</<SAPOrderNumber>
    </T>
    </L>
    <L>
    <RecordType>L</RecordType>
    <LoadNumber>3</LoadNumber>
    <T>
    <RecordType>T</RecordType>
    <SAPOrderNumber>ABC</<SAPOrderNumber>
    </T>
    <T>
    <RecordType>T</RecordType>
    <SAPOrderNumber>DEF</<SAPOrderNumber>
    </T>
    </L>

    <L>
    <RecordType>L</RecordType>
    <LoadNumber>4</LoadNumber>
    </L>

    </Record>

    Hope this is clear. If not please revert or email me on shameershaiks@gmail.com.

    Many Thanks.

    Shameer.


    Add comment
    10|10000 characters needed characters exceeded