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

Having trouble installing <oXygen/>? Got a bug to report? Post it all here.
yankee
Posts: 2

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

Tue Aug 02, 2016 7:33 pm

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?
radu_pisoi
Posts: 327
Location: Craiova

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

Wed Aug 03, 2016 11:26 am

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>
Radu Pisoi
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
yankee
Posts: 2

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

Wed Aug 03, 2016 3:17 pm

radu_pisoi wrote:Meanwhile, you can fix it in your distribution by opening the file:[..]

Thanks, that worked :-).
Dill
Posts: 7

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

Mon Sep 12, 2016 4:55 pm

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>
radu_pisoi
Posts: 327
Location: Craiova

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

Tue Sep 13, 2016 10:06 am

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.
Radu Pisoi
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Return to “Common Problems”

Who is online

Users browsing this forum: No registered users and 3 guests