Sortting and format a XML File

Questions about XML that are not covered by the other forums should go here.
Jose197
Posts: 6
Joined: Thu Dec 23, 2021 11:16 am

Sortting and format a XML File

Post by Jose197 »

Hello,
I am using XML Author and I have a problem, I have duplicate columns in this xml file. For example I have two "names" they appear duplicated because they are in different order throughout the xml. Attached screenshot.
image.png
image.png (15.74 KiB) Viewed 1719 times
I need to put these columns together so they don't appear separately. Is there any way the program can order this? The problem is that the file is somewhat large and doing it manually is tedious.

In a column there are some values ​​that are not defined and I need to be defined at least by the empty tags within the column. I know that if I double click them they are defined and appear in the xml. Is it possible to do it automatically? that is, add the missing tags.
image.png
image.png (2.25 KiB) Viewed 1719 times
I appreciate any response in advance.
Radu
Posts: 9055
Joined: Fri Jul 09, 2004 5:18 pm

Re: Sortting and format a XML File

Post by Radu »

Hi,

You could create an XSLT stylesheet to sort those elements in a certain order and to add new missing elements.
Maybe you can post an example of how an XML structure looks like and to what XML structure it should be converted.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Jose197
Posts: 6
Joined: Thu Dec 23, 2021 11:16 am

Re: Sortting and format a XML File

Post by Jose197 »

The structure is something like this:

Code: Select all

<data>
	<itemtype>
		<item classname="classname" id="1">
			<revision>61856</revision>
			<category>machine</category>
			<xdim>1</xdim>
			<ydim>1</ydim>
			<name>Namee</name>
			<description>Description</description>
			<adurl></adurl>
			<offerid>12</offerid>
			<excludeddynamic>0</excludeddynamic>
			<custom></custom>
			<specialtype>1</specialtype>
			<line>Line</line>
			<environment></environment>
		</item>
		</item>
		<item>....</item>
		<item>....</item>
        </itemtype>
</data>
What happens is that the rest have this same structure but some labels are out of order. And some faults. I want them all to have the same structure.
Radu
Posts: 9055
Joined: Fri Jul 09, 2004 5:18 pm

Re: Sortting and format a XML File

Post by Radu »

Hi,

For example an XSLT stylesheet which would sort all elements inside an <item> element could look like this:

Code: Select all

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:output indent="yes"/>
    <!-- Copy all elements as they are -->
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="item">
        <xsl:copy>
            <!-- Copy all attributes as they are -->
            <xsl:apply-templates select="@*"/>
            <!-- Sort its children elements and process them -->
            <xsl:for-each select="*">
                <xsl:sort select="local-name()"/>
                <xsl:apply-templates select="."/>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
You can create an XSLT transformation scenario in Oxygen to apply the XSLT stylesheet over the XML.

https://www.oxygenxml.com/doc/ug-editor ... -xslt.html

https://blog.oxygenxml.com/topics/xslt_training.html

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Jose197
Posts: 6
Joined: Thu Dec 23, 2021 11:16 am

Re: Sortting and format a XML File

Post by Jose197 »

Many thanks! it was exactly what I needed. Using xsl it can be done perfectly. I created the file and applied it to the XML and it did the sorting on its own perfectly.

Could I ask you for some more guidance? I want XSLT to add the missing tags in each element with a default value. I tried to do it using an if but when I apply it it doesn't work.

Code: Select all

<xsl:template match="item">
	<xsl:copy>
		<xsl:apply-templates select="node()|@*"></xsl:apply-templates>
		<xsl:if test="not(line)">
			<line>Default value</line>
		</xsl:if>
	</xsl:copy>
</xsl:template>
Maybe I'm doing something wrong but I've been trying to do it all afternoon. I really appreciate your time and kindness.
tavy
Posts: 365
Joined: Thu Jul 01, 2004 12:29 pm

Re: Sortting and format a XML File

Post by tavy »

Hello,

A solution ca be to generate all the item nodes in a variable and add also the missing elements and then sort the nodes. I'm not sure this is the best way to implement it, but you can try something like this:

Code: Select all

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
    <xsl:output indent="yes"/>
    <!-- Copy all elements as they are -->
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="item">
        <xsl:variable name="itemNodes">
            <xsl:if test="not(child::offerid)">
                <offerid>Default value</offerid>
            </xsl:if>
            <xsl:if test="not(child::adurl)">
                <adurl>Default value</adurl>
            </xsl:if>
            <xsl:apply-templates select="node()"/>
        </xsl:variable>
        
        <xsl:copy>
            <!-- Copy all attributes as they are -->
            <xsl:apply-templates select="@*"/>
            <!-- Sort its children elements-->
            <xsl:for-each select="$itemNodes/node()">
                <xsl:sort select="local-name()"/>
                <xsl:copy-of select="current()"/>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Best Regards,
Octavian
Octavian Nadolu
<oXygen/> XML Editor
http://www.oxygenxml.com
Jose197
Posts: 6
Joined: Thu Dec 23, 2021 11:16 am

Re: Sortting and format a XML File

Post by Jose197 »

Thank you very much for the reply. It has been very helpful and I have been able to put everything correctly. :D
Post Reply