Topic
11 replies Latest Post - ‏2014-06-10T11:45:42Z by Offline
Offline
Offline
211 Posts
ACCEPTED ANSWER

Pinned topic PrittyPrint XML?

‏2014-06-05T09:21:10Z |

Hi,
does anyone know if there is some "simple" way of Pretty Printing XML as output in DP?

I get a XML "string" as:

<root><elem1>val1</elem1><elem2>val2</elem2></root>

This is outputted to some old system that can't handle XML properly and they have requested to get one element per row, e.g.:

<root>
    <elem1>val1</elem1>
    <elem2>val2</elem2>
<root>

Any ideas?

Regards,

Anders

  • HermannSW
    HermannSW
    4238 Posts
    ACCEPTED ANSWER

    Re: PrittyPrint XML?

    ‏2014-06-05T09:46:41Z  in response to Offline

    You may start with "store:///prettyprint.xsl" (that does output HTML) ...

    Otherwise you need to specify more:

    • what about comments, processing instructions?
    • how should deeper elements be formatted?
    • what about attribute handling?
    • ...


    Hermann.

    • Offline
      Offline
      211 Posts
      ACCEPTED ANSWER

      Re: PrittyPrint XML?

      ‏2014-06-05T09:54:39Z  in response to HermannSW

      It is a MQ RFH2 that is needed to be handled:

      <MQRFH2><StrucId>RFH</StrucId><Version>2</Version><Format>MQSTR</Format><StrucLength/><Encoding>546</Encoding><CodedCharSetId>819</CodedCharSetId><Flags>0</Flags><NameValueCCSID>1208</NameValueCCSID><NameValueData><NameValue><usr><ContractId>DPLOG</ContractId><DP-TransactionId>29928079</DP-TransactionId><Logger-service>MPGW_ROUTE</Logger-service><Logger-policy>PP_RouteMsg</Logger-policy><TPC>600</TPC><routing-url>dpmq://QMXI52B/FSH_MQ_RTD?RequestQueue=RTD</routing-url><BASELINE_Sender_MsgID>ESIS027_A</BASELINE_Sender_MsgID><MQ-MsgId>414d5120514d58493532422020202020ce7b49532000bf05</MQ-MsgId><PutDate>20140603</PutDate><PutTime>09374949</PutTime></usr></NameValue></NameValueData></MQRFH2>
      
      
      

      Ideally it would be normal "pritty print", like:

      <MQRFH2>
      <StrucId>RFH</StrucId>
      <Version>2</Version>
      <Format>MQSTR</Format>
      <StrucLength/>
      <Encoding>546</Encoding>
      <CodedCharSetId>819</CodedCharSetId>
      <Flags>0</Flags>
      <NameValueCCSID>1208</NameValueCCSID>
      <NameValueData>
      <NameValue>
      <usr>
      <ContractId>DPLOG</ContractId>
      <DP-TransactionId>29928079</DP-TransactionId>
      <Logger-service>MPGW_ROUTE</Logger-service>
      <Logger-policy>PP_RouteMsg</Logger-policy>
      <TPC>600</TPC>
      <routing-url>dpmq://QMXI52B/FSH_MQ_RTD?RequestQueue=RTD</routing-url>
      <BASELINE_Sender_MsgID>ESIS027_A</BASELINE_Sender_MsgID>
      <MQ-MsgId>414d5120514d58493532422020202020ce7b49532000bf05</MQ-MsgId>
      <PutDate>20140603</PutDate>
      <PutTime>09374949</PutTime>
      </usr>
      </NameValue>
      </NameValueData>
      </MQRFH2>

      There are no attributes used in RFH2 headers (I think) so that wouldn't be an issue...

      It would also be enough with just line-breaking each element as a first step...

      Thanks Hermann!

      Regards,

      Anders

      Updated on 2014-06-05T10:00:39Z at 2014-06-05T10:00:39Z by Offline
      • HermannSW
        HermannSW
        4238 Posts
        ACCEPTED ANSWER

        Re: PrittyPrint XML?

        ‏2014-06-05T12:11:21Z  in response to Offline

        Hi Anders,

        I started with "store:///prettyprint.xsl" which produced attached HTML output mqrfh2.xml.html for your input.

        Then I did

        • replace <xsl:text> output for elements by <xsl:element>
        • removed tha "kludge" by  str:padding()
        • only generated newline and indent for new element ...
        • ... and for element closing tag in case last contained node() is not text
          (nice use of generate-id() )
        • made start indent 0 (*3)

         

        Find attached  pprint.xsl, and see output for your sample input here:

        $ coproc2 pprint.xsl mqrfh2.xml http://dp2-l3:2223; echo
        
        <MQRFH2>
           <StrucId>RFH</StrucId>
           <Version>2</Version>
           <Format>MQSTR</Format>
           <StrucLength/>
           <Encoding>546</Encoding>
           <CodedCharSetId>819</CodedCharSetId>
           <Flags>0</Flags>
           <NameValueCCSID>1208</NameValueCCSID>
           <NameValueData>
              <NameValue>
                 <usr>
                    <ContractId>DPLOG</ContractId>
                    <DP-TransactionId>29928079</DP-TransactionId>
                    <Logger-service>MPGW_ROUTE</Logger-service>
                    <Logger-policy>PP_RouteMsg</Logger-policy>
                    <TPC>600</TPC>
                    <routing-url>dpmq://QMXI52B/FSH_MQ_RTD?RequestQueue=RTD</routing-url>
                    <BASELINE_Sender_MsgID>ESIS027_A</BASELINE_Sender_MsgID>
                    <MQ-MsgId>414d5120514d58493532422020202020ce7b49532000bf05</MQ-MsgId>
                    <PutDate>20140603</PutDate>
                    <PutTime>09374949</PutTime>
                 </usr>
              </NameValue>
           </NameValueData>
        </MQRFH2>
        $
        

         

        Hermann <myBlog/> <myTweets/> | <GraphvizFiddle/> | <xqib/> | <myCE/> <myFrameless/>

         

        Attachments

        • Offline
          Offline
          211 Posts
          ACCEPTED ANSWER

          Re: PrittyPrint XML?

          ‏2014-06-05T13:09:46Z  in response to HermannSW

          My God, Hermann, someone should really crown you King! :)

          Thanks!

          Regards,

          Anders

        • Offline
          Offline
          211 Posts
          ACCEPTED ANSWER

          Re: PrittyPrint XML?

          ‏2014-06-09T05:30:50Z  in response to HermannSW

          Hmmm... I ran into some issues anyway that I can't seem to solve (or maybe working late on a Sunday evening is not the best of ideas...?)...

          I am creating a log message where I need to add your parsed MQRHF2 on top of the original paypload, like:

          <MQRFH2>
          xxxx
          ----------------------
          ---Payload---

          Payload can be either XML or "binary" so I am treating it all as "binary".

          I have your parsed XML in a variable but when I place it on top of the MQ message the parsing is removed and then I get it as a regular "string" again... :(

          So, the question is then, how can I get from parsed, pritty-printed XML into a pritty-printed "XML string"?

          Regards,

          Anders

          • HermannSW
            HermannSW
            4238 Posts
            ACCEPTED ANSWER

            Re: PrittyPrint XML?

            ‏2014-06-09T13:35:44Z  in response to Offline

            > I am creating a log message where I need to ...
            >
            How?
            Log action, or via <xsl:message>?


            Hermann.

            • Offline
              Offline
              211 Posts
              ACCEPTED ANSWER

              Re: PrittyPrint XML?

              ‏2014-06-09T17:30:44Z  in response to HermannSW

              I am receiving the payload over HTTP along with the MQRFH2 as a HTTP header into a MPGW.

              I then need to add the header on top of the Payload and output it onto a MQ queue (that ends up in a monitoring system).

              Regards,

              Anders

              • HermannSW
                HermannSW
                4238 Posts
                ACCEPTED ANSWER

                Re: PrittyPrint XML?

                ‏2014-06-09T17:40:53Z  in response to Offline

                OK, so no logging, but just "normal" output to MQ.

                Please do not store pretty printed XML in a variable.
                Just generate output to (MQ) backend by a xform action with output context OUTPUT.

                In your top level template just call pretty print first, and then generate the rest of your output.
                ( <xsl:call-template name="element">... )


                Hermann <myBlog/> <myTweets/> | <GraphvizFiddle/> | <xqib/> | <myCE/> <myFrameless/>

                 

                • Offline
                  Offline
                  211 Posts
                  ACCEPTED ANSWER

                  Re: PrittyPrint XML?

                  ‏2014-06-09T18:03:48Z  in response to HermannSW

                  I need to add the original payload as well... e.g.:

                  Output message could look like:

                  <MQRFH2>
                      <Element></<Element>
                      <Element></<Element>
                  </MQRFH2>
                  -----------------------------------
                  UNB+123456:78+7865432:14+ABC'UNH+ORDERS...

                  Thus I need to get the pretty-printend MQRFH2 header and also add the original payload below it...