Page 1 of 1
Sortting and format a XML File
Posted: Thu Dec 23, 2021 11:41 am
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
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
I appreciate any response in advance.
Re: Sortting and format a XML File
Posted: Thu Dec 23, 2021 12:07 pm
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
Re: Sortting and format a XML File
Posted: Thu Dec 23, 2021 1:03 pm
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.
Re: Sortting and format a XML File
Posted: Thu Dec 23, 2021 3:49 pm
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
Re: Sortting and format a XML File
Posted: Fri Dec 24, 2021 1:38 am
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.
Re: Sortting and format a XML File
Posted: Fri Dec 24, 2021 2:00 pm
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
Re: Sortting and format a XML File
Posted: Fri Dec 24, 2021 2:20 pm
by Jose197
Thank you very much for the reply. It has been very helpful and I have been able to put everything correctly.
