Convert elements to attributes

Questions about XML that are not covered by the other forums should go here.
bee
Posts: 1
Joined: Sun May 15, 2016 11:09 pm

Convert elements to attributes

Post by bee »

Hello,

I am fairly new to oxygen. I now have received a file without a schema and plenty of other oddities.
I'd like to change it a bit to begin with. Can't figure out how to change the structure from:

Code: Select all

	<article>
<header>
<title></title>
<author></author>
<date></date>
</header>
<body>
<text>Überschrift des Artikels</text>
</body>
</article>
to

Code: Select all

	<article title="" author="" date="">
<text> </text>
</article>
Any help is much appreciated!
Thanks
b
adrian
Posts: 2879
Joined: Tue May 17, 2005 4:01 pm

Re: Convert elements to attributes

Post by adrian »

Hi.

Oxygen does not have a specialized operation for this.

You can use an XSL stylesheet that does this:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<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 method="xml" indent="yes"/>
<!-- Match document -->
<xsl:template match="/">
<xsl:apply-templates mode="copy" select="."/>
</xsl:template>
<!-- Deep copy template -->
<xsl:template match="*|text()|@*" mode="copy">
<xsl:copy>
<xsl:apply-templates mode="copy" select="@*"/>
<xsl:apply-templates mode="copy"/>
</xsl:copy>
</xsl:template>
<!-- Handle default matching -->
<xsl:template match="*"/>

<!-- Convert-->
<xsl:template match="article" mode="copy">
<xsl:copy>
<xsl:apply-templates mode="convert-to-attr" select="header/*"/>
<xsl:apply-templates mode="copy" select="body/*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*" mode="convert-to-attr">
<xsl:attribute name="{name()}" select="text()"/>
</xsl:template>
</xsl:stylesheet>
First part is a generic copy stylesheet (found in Oxygen/samples/xhtml/copy.xsl). The second part (Convert) addresses your specific situation, converts the elements from 'header' into attributes of the same name and copies the element from the 'body' element.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
adrian
Posts: 2879
Joined: Tue May 17, 2005 4:01 pm

Re: Convert elements to attributes

Post by adrian »

Create a transformation scenario in order to apply a stylesheet in Oxygen
To set up a transformation scenario, open the XML file and from the main menu invoke Document -> Transformation -> "Configure Transformation Scenario(s)" (there's a corresponding button in the toolbar)

In the "Configure Transformation Scenario(s)" dialog press "New" and select "XML transformation with XSLT" to create a new scenario:
1. Give it an appropriate name
2. Leave the "XML URL" field to its default(${currentFileURL})
3. In the XSL URL field browse for your stylesheet.
4. If you have an XSLT 2.0 stylesheet choose from the transformer combo Saxon-PE or Saxon-EE, otherwise you can leave it to Saxon6.5.5.
5. You can further tune the "Output". Please note that the "Save as" field must refer a single file, NOT an output directory. Use the editor variables to compose a generic name instead of a fixed one.
e.g in the "Save as" field you can specify: ${cfd}/${cfn}-out.xml which translates into <current-file-directory>/<current-filename>-out.xml
6. Press OK in the editing dialog and "Save and close".
7.To apply this transformation use : Document -> Transformation -> "Apply Transformation Scenario(s)" and the associated scenario will be used.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Post Reply