Question
· Oct 10, 2017

How to project XML to Nested Xml

I have an xml file that I read into objects and I am trying to print that file back in nested xml anyone with idea on how that can be achieved or where I can read in order to achieve this, here is an example of what I am trying to do.

From this

 

<Root>
  <FamilyMembers>
  <familyName>
  </familyName>
  <Name>
  </Name>
  <Age>
  </Age>
  <Employment>
  </Employment>
  </FamilyMembers>
</Root>

 

To this

 

 

<?xml version="1.0" encoding="UTF-8"?>
<Root>
  <FamilyMembers>
      <Family>
     <familyName>
        </familyName>
      </Family>
    <Name>
    </Name>
    <Age>
    </Age>
    <Employment>
    </Employment>
  </FamilyMembers>
</Root>
Discussion (2)1
Log in or sign up to continue

Hi Thembelani,

Assuming that you have got as far as serialising the objects to an XML stream, and you just want to prettify the XML, then you can use the following solution.

Add the following to a new / existing class...
 

ClassMethod Format(pXmlStream As %CharacterStream, Output pXmlStreamFormatted As %CharacterStream) As %Status
{
    set xslt=##class(%Dictionary.XDataDefinition).%OpenId(..%ClassName(1)_"||XSLT",-1,.sc)
    quit ##class(%XML.XSLT.Transformer).TransformStream(pXmlStream,xslt.Data,.pXmlStreamFormatted)
}

XData XSLT
{
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>
 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>
}

To use it...

set sc=##class(Your.Class).Format(.xmlStream,.xmlStreamFormatted)


Pass in your XML stream as the first argument (by ref) and get back the formatted stream as the second argument (by ref).