XML refactoring: wrap element

Questions about XML that are not covered by the other forums should go here.
joerg.hambuch
Posts: 18

XML refactoring: wrap element

Thu May 18, 2017 4:35 am

Hi,

I try to wrap several elements with another one using the Wrap Element operation but I don't get the expected result.

My XML looks like this:

Code: Select all

<x>
  <y-a>some text</y-a>
  <y-b>some text</y-b>
  <y-c>some text</y-b>
</x>


What I like to get is the following:

Code: Select all

<x>
  <y>
    <y-a>some text</y-a>
    <y-b>some text</y-b>
    <y-c>some text</y-b>
  </y>
</x>


As Target Elements I entered the following XPath: y-a, y-b, y-c
As Local Name I entered: y

What I get is:

Code: Select all

<x>
  <y>
    <y-a>some text</y-a>
  </y>
  <y>
    <y-b>some text</y-b>
  </y>
  <y>
    <y-c>some text</y-c>
  </y>
</x>


Could you please advise what XPath expression I should enter as Target Elements to get my expected result?

Follow-up question: What should the XPath expression look like, if I have the mentioned XML code in several files and in some of them there is no <y-b> , but <y-a> and <y-c> only?

Thanks a lot,
Jörg
adrian
Posts: 2326

Re: XML refactoring: wrap element

Thu May 18, 2017 12:30 pm

Hello,

You seem to be using the wrong operation, if you want to wrap all (not each) of the elements from "x" (all its content) with a single element "y".
"Wrap element" wraps each of the elements specified by the XPath with the specified wrapper element.
You need to use the "Wrap element content" operation and specify the parent of those elements in the XPath, "x".

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
joerg.hambuch
Posts: 18

Re: XML refactoring: wrap element

Thu May 18, 2017 1:15 pm

Hi,

thanks for your reply. Sorry, I missed a important detail in my post: The XML contains elements on the same level, and those elements should not be wrapped.

The code looks like this:

Code: Select all

<x>
  <element_that_should_not_be_wrapped>some text</element_that_should_not_be_wrapped>
  <y-a>some text</y-a>
  <y-b>some text</y-b>
  <y-c>some text</y-b>
  <element_that_also_should_not_be_wrapped>some text</element_that_also_should_not_be_wrapped>
</x>


Any idea?

Thanks,
Jörg
adrian
Posts: 2326

Re: XML refactoring: wrap element

Thu May 18, 2017 6:53 pm

Hi,

I'm afraid you can't do that one with the predefined XML Refactoring actions.
You'll need a Custom Refactoring Operation or an XSLT stylesheet.

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

Return to “General XML Questions”

Who is online

Users browsing this forum: No registered users and 0 guests