Page 1 of 1

XML refactoring: wrap element

Posted: Thu May 18, 2017 4:35 am
by joerg.hambuch
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

Re: XML refactoring: wrap element

Posted: Thu May 18, 2017 12:30 pm
by adrian
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

Re: XML refactoring: wrap element

Posted: Thu May 18, 2017 1:15 pm
by joerg.hambuch
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

Re: XML refactoring: wrap element

Posted: Thu May 18, 2017 6:53 pm
by adrian
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