Abdul Hammed
Abdul Hammed

Reputation: 1

add parent node for multiple child which are in same hierarchy of XMLusing XSLT

i have a scenario where my input is as below which is invalid Xml to parse and expected output added below kindly help me get the desired output

<MessageHeader>
     <ServiceInitiatorKey>000</ServiceInitiatorKey>
     <ServiceProviderKey>SADAD-001</ServiceProviderKey>
     <ServiceConsumerId>000</ServiceConsumerId>
     <RqUID>4lkuo5bi-8e98-8bnn-32ge-ep3a6eiss241</RqUID>
     <Date>2020-00-00T00:00:00</Date>
     <Lang>en-gb</Lang>
  </MessageHeader><LoadRq>
     <Timestamp>2020-00-00T00:00:00</Timestamp>
     <Payment>
        <BillerId>000</BillerId>
        <CurAmt>0</CurAmt>
     
        <PrcDt>2020-00-00T00:00:00</PrcDt>
        <DueDt>2020-00-00T00:00:00</DueDt>
        <BillerPmtId>000000</BillerPmtId>
        <PaymentRef>
           <BillNumberWithAccount>
              <BillingAcct>0000000000</BillingAcct>
              <BillNumber>0000000000</BillNumber>
           </BillNumberWithAccount>
           
        </PaymentRef>
     </Payment>
  </LoadRq>

and expected output is as below

<?xml version="1.0" encoding="UTF-8"?>
<root>
<MessageHeader>
     <ServiceInitiatorKey>000</ServiceInitiatorKey>
     <ServiceProviderKey>SADAD-001</ServiceProviderKey>
     <ServiceConsumerId>000</ServiceConsumerId>
     <RqUID>4lkuo5bi-8e98-8bnn-32ge-ep3a6eiss241</RqUID>
     <Date>2020-00-00T00:00:00</Date>
     <Lang>en-gb</Lang>
  </MessageHeader><LoadRq>
     <Timestamp>2020-00-00T00:00:00</Timestamp>
     <Payment>
        <BillerId>000</BillerId>
        <CurAmt>0</CurAmt>
     
        <PrcDt>2020-00-00T00:00:00</PrcDt>
        <DueDt>2020-00-00T00:00:00</DueDt>
        <BillerPmtId>000000</BillerPmtId>
        <PaymentRef>
           <BillNumberWithAccount>
              <BillingAcct>0000000000</BillingAcct>
              <BillNumber>0000000000</BillNumber>
           </BillNumberWithAccount>
           
        </PaymentRef>
     </Payment>
  </LoadRq>

Upvotes: 0

Views: 68

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

In XSLT/XPath 3 there is a function parse-xml-fragment so using e.g.

<xsl:template name="xsl:initial-template">
  <root>
    <xsl:sequence select="parse-xml-fragment(unparsed-text('your-file.xml'))"/>
  </root>
</xsl:template>

should allow you to add a wrapper element. You would start the transformation with the named template (e.g. -it command line options for Saxon 9/10).

Upvotes: 0

Related Questions