Reformat XML for a specific use (using XSLT)

When using the XMLWriter with Linx, one can create really well-formed XML documents. However, there are different standards of XML documents used in the industry today. For that you need to transform a document to fit into your required form.

The explanations are mostly for Linx5 users. We`ve included a sample for Linx6 users.

Feel free to contact support@linx.software and we'll assist.



Please note the terms ‘Process’ and ‘Custom Type’ have been depreciated and have been replaced with ‘Function’ and ‘Type’ respectively. More details here.

As a recent example shows, an XML document was needed for a structure of a document. The structure was created by using Custom Types in Linx:

This structure was loaded with some data and fed into an XMLWriter.

The resultant XML was well-formed, however, the required structure needed that the list of Document Info tags (DocInf) be shown an separate DocInf objects, not Item objects, and also no empty tags were allowed. The plain XML looked like this:

Now, its here where Linx provides a very nice ability, to use XSLT (Extensible Stylesheet Language Transformations) to change your XML in any way possible. For this example we’ve set up an XSLT script that removed the overarching “DocInf” tag, changed the “Item” tags to “DocInf” tags and removed all empty tags:

  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="no" method="xml" indent="yes" encoding="UTF-8"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()[not(self::DocInf)]|@*">
    <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="DocInf/Item">
        <DocInf><xsl:apply-templates select="@*|node()" /></DocInf>
   </xsl:template>


  <xsl:template match=
     "*[not(@*|*|comment()|processing-instruction()) 
     and normalize-space()=''
     ]"/>

  </xsl:stylesheet>

This XSLT template then changed the XML to adhere to the required form:

1 Like

@Dawie_Botes this is a great post… can you attach a Linx solution example

Hi @brianwhite ,

Linx 5 - ReformatXMLDemo.lsoz (9.1 KB)

Linx 6 - Linx6.zip (8.0 KB)

Using a recent example, an XML document was needed for a structure of a document. The structure was created by using Custom Types in Linx:

image

These are ComplexTypes which contain further ComplexTypes which creates the “nesting” of the objects:

First, we created a local instance of the main CustomType ‘Customer’, which we then populated manually.

The ComplexType ‘Document’ object is then passed into the XMLWriter function which generates and XML output, which is then written to a file using the TextFileWrite function.

image

The output is as the below:

The XML is well-formed, however the required structure needed is that the list of Document Info tags (DocInf) be shown in separate DocInf objects, not Item objects, and also no empty tags were allowed.

Now, it’s here where Linx provides a very nice ability, by utilizing XSLT (Extensible Stylesheet Language Transformations) you can change your XML in any way possible. For this example, we’ve set up an XSLT script (in the above thread) that removes the overarching “DocInf” tag, changed the “Item” tags to “DocInf” tags and removed all empty tags.

In the XmlWriter function, assign the above script to the ‘Transform XSLT’ property.
image

When you run the process, you will see that this XSLT template changes the XML to adhere to the required form (right):

image

Thank you Ronan. This is a great example. :grinning: