Page 1 of 1

Using XPath to refactor xml in oxygen

Posted: Wed Aug 09, 2017 10:18 am
by brentclark2003
have an xml doc that I am trying to reorganize and reformat. I have been trying to use oxygen's refactoring tool to wrap specific elements or group of elements. But instead of grouping the desired elements together, it just wraps each matched element individually. How do I write an XPath expression that targets and groups elements in this manner:

Using the Wrap Element tool to wrap the first 3 chunk elements together in an s element.

Going from this:

Code: Select all

<system>
<n>1057</n>
<chunk>X1</chunk>
<chunk>C6HF5</chunk>
<chunk>Pentafluorbenzol</chunk>

<chunk>C6H2F41,2,3,5-Tetrafluorbenzol</chunk>
<chunk>Äquimolare Mischung: </chunk>
<chunk/>
<r>[F5]</r>
<c>25°C</c>
<chunk>ΔHM = 1,84 cal/mol Misch. = 7,7 J/mol Misch.</chunk>
To this

Code: Select all

<system>
<n>1057</n>
<s><chunk>X1</chunk>
<chunk>C6HF5</chunk>
<chunk>Pentafluorbenzol</chunk></s>

<chunk>C6H2F41,2,3,5-Tetrafluorbenzol</chunk>
<chunk>Äquimolare Mischung: </chunk>
<chunk/>
<r>[F5]</r>
<c>25°C</c>
<chunk>ΔHM = 1,84 cal/mol Misch. = 7,7 J/mol Misch.</chunk>
Using the the XPath expression: /systems/system/chunk[position()<4] to select the first 3 chunk elements and enclose them with s tags. However when I run this in the refactoring command I get this:

Code: Select all

<system>
<n>1057</n>
<s><chunk>X1</chunk></s>
<s><chunk>C6HF5</chunk></s>
<s><chunk>Pentafluorbenzol</chunk></s>

<chunk>C6H2F41,2,3,5-Tetrafluorbenzol</chunk>
<chunk>Äquimolare Mischung: </chunk>
<chunk/>
<r>[F5]</r>
<c>25°C</c>
<chunk>ΔHM = 1,84 cal/mol Misch. = 7,7 J/mol Misch.</chunk>
Where it selects the proper chunks, but wraps each chunk individually. Please let me know if you have any suggestions on how to group all elements together in s tags. Or if I shouldn't even be using oxygen xml to accomplish this.

Re: Using XPath to refactor xml in oxygen

Posted: Wed Aug 09, 2017 10:41 am
by sorin_carbunaru
Hello,

oXygen does not have a default XML Refactoring operation for what you need. But you could define your own operation, if you'd like (see https://www.oxygenxml.com/doc/versions/ ... efactoring).

As the operation's script you could use Martin Honnen's XSLT from https://stackoverflow.com/questions/454 ... -in-oxygen:

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="3.0">

<xsl:mode on-no-match="shallow-copy"/>

<xsl:template match="system/chunk[1]">
<s>
<xsl:copy-of select="., following-sibling::chunk[position() lt 3]"/>
</s>
</xsl:template>

<xsl:template match="system/chunk[2] | system/chunk[3]"/>
</xsl:stylesheet>
You cannot write an XPath expression for the Wrap element operation to get what you want, because XPath only selects nodes from the XML, and cannot group them.

Regards,
Sorin Carbunaru
oXygen XML