Page 1 of 1

How to delete an attribute from some elements selected by XPath?

Posted: Tue Aug 02, 2016 7:33 pm
by yankee
I have an XML document which contains structures such as this one:

Code: Select all

<a x="">
<Code>short</Code>
</a>
<a x="">
<Code>looooooonnnngggggggg</Code>
</a>
Now from all Elements "a" I want to delete the attribute "x" if the text of "Code" has no lines with more than 10 characters. Or in short: I want to delete

Code: Select all

//a[Code[not(matches(text(), '[^\n]{10,}'))]]/@x
from my document.

If I enter the above XPath-Expression into the XPath-Search box I only get a single result: The attribute that I want to delete.

In the menu, I found the Option Tools->XML Refactoring->Attributes->Delete attribute. I can specify an XPath expression there from which an attribute can be deleted. Great! Except that my conditions are completely ignored. Oxygen will always delete all attributes "x" in the whole document. It will even remove the attribute "x" from "<b x="" />". It feels like the XPath is completely ignored. However if I supply an XPath that yields zero nodes in the whole document, no changes are performed.

What is going on there and how can I do what I would like to do?

Re: How to delete an attribute from some elements selected by XPath?

Posted: Wed Aug 03, 2016 11:26 am
by radu_pisoi
Hi,

Thank you for reporting this problem.

It seems to be an issue in the XSLT stylesheet used to implement this operation. We already fixed this problem and the next minor bug fixes release will include a fix for it.
Meanwhile, you can fix it in your distribution by opening the file:

Code: Select all


{oXygenInstallDir}/refactoring/delete-attribute.xsl
and replace the code

Code: Select all


<xsl:if test="not(.=$attributes)">
<xsl:copy/>
</xsl:if>
with code

Code: Select all


<xsl:if test="not(. intersect $attributes)">
<xsl:copy/>
</xsl:if>

Re: How to delete an attribute from some elements selected by XPath?

Posted: Wed Aug 03, 2016 3:17 pm
by yankee
radu_pisoi wrote:Meanwhile, you can fix it in your distribution by opening the file:[..]
Thanks, that worked :-).

Re: How to delete an attribute from some elements selected by XPath?

Posted: Mon Sep 12, 2016 4:55 pm
by Dill
You could also use xquery update for tasks like this.
In the xquery builder select saxon EE and in settings check "enable xq update" and "use linked tree model" and enter the code

Code: Select all

delete nodes doc(<your-document>)/<your-xpath>

Re: How to delete an attribute from some elements selected by XPath?

Posted: Tue Sep 13, 2016 10:06 am
by radu_pisoi
Hi,

Yes, you are right, you can use XQuery update technology to update XML files. The XML Refactoring tool is implemented by running an XQuery update or XSLT operation over a set of XML files.

However, the XML Refactoring tool has some advantages as follow:
  • The DOCTYPE will be preserved.
  • The DTD entities will be preserved as they are in the original document when the document is saved.
  • The attribute values will be kept in their original form without being normalized.
  • The spaces between attributes are preserved. Basically, the spaces are lost by a regular XML serialization since they are not considered important.