Sortting and format a XML File
Questions about XML that are not covered by the other forums should go here.
-
- Posts: 6
- Joined: Thu Dec 23, 2021 11:16 am
Sortting and format a XML File
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.
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.
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.You do not have the required permissions to view the files attached to this post.
-
- Posts: 9423
- Joined: Fri Jul 09, 2004 5:18 pm
Re: Sortting and format a XML File
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
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
<oXygen/> XML Editor
http://www.oxygenxml.com
-
- Posts: 6
- Joined: Thu Dec 23, 2021 11:16 am
Re: Sortting and format a XML File
The structure is something like this:
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.
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>
-
- Posts: 9423
- Joined: Fri Jul 09, 2004 5:18 pm
Re: Sortting and format a XML File
Hi,
For example an XSLT stylesheet which would sort all elements inside an <item> element could look like this:
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
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>
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
<oXygen/> XML Editor
http://www.oxygenxml.com
-
- Posts: 6
- Joined: Thu Dec 23, 2021 11:16 am
Re: Sortting and format a XML File
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.
Maybe I'm doing something wrong but I've been trying to do it all afternoon. I really appreciate your time and kindness.
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>
-
- Posts: 387
- Joined: Thu Jul 01, 2004 12:29 pm
Re: Sortting and format a XML File
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:
Best Regards,
Octavian
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>
Octavian
Octavian Nadolu
<oXygen/> XML Editor
http://www.oxygenxml.com
<oXygen/> XML Editor
http://www.oxygenxml.com
Return to “General XML Questions”
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service