Linx Main Linx Help

Reformat XML for a specific use (using XSLT)

#1

When using the XMLWriter with Linx 5, 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.

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

#2

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

0 Likes

#3

Hi @brianwhite ,

I have attached a sample: ReformatXMLDemo.lsoz (9.1 KB)

The sample was created in Linx v5.15.10755.0, to open the attached solution you will need a version equal or later than the above. I have attached screenshots for those who are still using an older version.

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

0 Likes

#4

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

0 Likes