Question
· Mar 26, 2017

How to split an xml into 3 xml

Hi,

I have a xml stored in a globalCharrachter Stream.

I would like to get a full node parts of the xml to store it in an other file.

Exemple of an Xml store in cache

<doc>
<header></header>
<product></header>
<market></marker>
<doc>
</aaa>

I would like to generate new xml from this one like

<product></product> to product.xml

<market></market> to market.xml

s xml = ##class(MyExemple.test).%OpenId(1) --> my object with XML <doc> store 
s doc = ##class(%XML.Document).%New()
d doc.GetDocumentFromStream(xml.TXML)
Discussion (3)0
Log in or sign up to continue

you should look at XSLT, it can help transform xml as you need

ClassMethod SplitXML() 
{

    for item="market","product" {
        set params("path")="/doc/"_item
        
        Set tSource=##class(%Dictionary.CompiledXData).%OpenId(..%ClassName(1)_"||XML").Data
        
        Set tXSL=##class(%Dictionary.CompiledXData).%OpenId(..%ClassName(1)_"||XSL").Data

        // Transform the source according to the XSL
        Set tSC=##class(%XML.XSLT.Transformer).TransformStream(tSource,tXSL,.tOutput,,.params)
        If $$$ISERR(tSC) Quit
        
        write !!
        // Output the result to the screen
        Set tSC=tOutput.OutputToDevice()
    }
}

XData XML
{
<?xml version="1.0"?>
<doc>
<header></header>
<product><test>1</test></product>
<market><test2>2</test2></market>
</doc>
}

XData XSL
{
<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:param name="path"></xsl:param>

<xsl:output method="xml" indent="no"/>
 
<xsl:template match="/">
    <xsl:copy-of select="$path"/>
</xsl:template>
    
</xsl:stylesheet>
}

will output like below

USER>d ##class(Test).SplitXML()


<?xml version="1.0" encoding="UTF-8"?><market><test2>2</test2></market>

<?xml version="1.0" encoding="UTF-8"?><product><test>1</test></product>