Add xml:lang attribute to multiples files at once

Post here questions and problems related to editing and publishing DITA content.
rminaker
Posts: 27
Joined: Thu Dec 19, 2013 4:34 pm

Add xml:lang attribute to multiples files at once

Post by rminaker »

Hi,

Is there any way that I can add the xml:lang attribute to a whole bunch of DITA files at once?

As an example, I have a whole set of French DITA topics without an xml:lang specified. So I'd like to be able to add xml:lang="fr-ca" to them all at once, rather than each file one by one.

I thought there might be a way to trick Find/Replace into doing this, but I don't believe there is.

Thanks!
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Add xml:lang attribute to multiples files at once

Post by adrian »

Hi,

While there still is no specialized action that accomplishes this, you can use the Find/Replace in Files with a bit of tweaking.

There was a similar question on our oXygen user discussion list in November 2013:
[oXygen-user] Applying xml:lang across every file in map
I'm quoting the solution here for convenience. This assumes you have a DITA map open in the DITA Maps Manager and want to change/set the xml:lang for all topics from this map. However, you can use this same method with a different scope (Project selection, opened files, specific directory, etc).
If you know you don't have xml:lang set in any of the topics, you can skip step 2.
Steps:
1. First create (or make sure you have) a backup copy of your files (preferably the entire project), just in case anything goes wrong.

2. If you already have xml:lang set on the root element of some of the files, you should first remove them (otherwise skip to step 3):
In the DITA Maps Manager right click on the map root and from the contextual menu pick Find/Replace in Files. Fill the fields this way:
Text to find: .*
Regular expression: Enabled
Restrict to XPath: /*/@xml:lang
Replace with: (empty)
Scope: Current DITA Map hierarchy
Include files: *.*
Press Replace All... and Preview the operation to make sure that it does what is expected of it (it should remove the existing xml:lang attributes).

3. To add the xml:lang set on the root element of the files after the last attribute, use again the Find/Replace in Files and use the following field values:
Text to find: .*
Regular expression: Enabled
Restrict to XPath: /*/@*[last()]
Replace with: $0 xml:lang="en-US" (modify the value to the one you want to set)
Scope: Current DITA Map hierarchy
Include files: *.*
Press Replace All... and Preview the operation to make sure that it does what is expected of it (xml:lang should be added after the last attribute of each root element ).
Regards,
Adrian

Later Edit: Corrected spelling error, "en-US" instead of "en_US".
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
cg.tw75
Posts: 14
Joined: Tue Jun 30, 2015 3:43 pm

Re: Add xml:lang attribute to multiples files at once

Post by cg.tw75 »

Hello,
I'm trying to use the method described above to set the attribute value for all my dita files, but I get a message saying that the regular expression is invalid. I'm using Oxygen 17.0. Has something changed?
Thanks,
C.
Radu
Posts: 9053
Joined: Fri Jul 09, 2004 5:18 pm

Re: Add xml:lang attribute to multiples files at once

Post by Radu »

Hi,

Could you precisely tell me what values you have set for the "text to find" and "text to replace" fields?
Also as another approach Oxygen 17.0 and newer have support for XML refactory actions. There are already predefined actions to remove or to set attributes using an XPath expression:

https://www.oxygenxml.com/demo/XML_Refactoring.html

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
cg.tw75
Posts: 14
Joined: Tue Jun 30, 2015 3:43 pm

Re: Add xml:lang attribute to multiples files at once

Post by cg.tw75 »

Hi Radu,
Thanks for your reply.

In fact the regular expression was not invalid, I had missed the . before the / in the first field. However, the results were not as expected.xml:lang and fr-fr were removed, but ="" was left, making the xml invalid. Maybe I made another mistake? In the text to find box I had .*, in Restrict to XPath I had /*/@xml:lang, and I had nothing in Replace with. All options were selected in Enable XML search options.

I also tried the refactoring command. I encountered the following problems:
- The attribute xml:lang was not recognized.
- I tried inserting another attribute just to see if it worked. It did, but when Oxygen parsed my files, it also removed all CDATA elements and replaced all < and > characters by entities in my codeblock elements.
-I had the following error message in the messages pane: "The prefix "ditaarch" for attribute "ditaarch:DITAArchVersion" associated with an element type "bookmap" is not bound".

Thank you for your help!
C.
radu_pisoi
Posts: 403
Joined: Thu Aug 21, 2003 11:36 am
Location: Craiova
Contact:

Re: Add xml:lang attribute to multiples files at once

Post by radu_pisoi »

cg.tw75 wrote:- I tried inserting another attribute just to see if it worked. It did, but when Oxygen parsed my files, it also removed all CDATA elements and replaced all < and > characters by entities in my codeblock elements.
Yes, this is the way how the XML Refactoring tool works. The CDATA section are expanded/replaced with their escaped content. Please note that both representations are XML equivalent. I will register this problem in our issue tracking system to be analysed in a next oXygen version.
cg.tw75 wrote:I also tried the refactoring command. I encountered the following problems:
- The attribute xml:lang was not recognized.
-I had the following error message in the messages pane: "The prefix "ditaarch" for attribute "ditaarch:DITAArchVersion" associated with an element type "bookmap" is not bound".
I was not able to reproduce these problem with a small DITA map. Could you send us at support@oxygenxml.com a small DITA map to reproduce this problem?
Radu Pisoi
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
cg.tw75
Posts: 14
Joined: Tue Jun 30, 2015 3:43 pm

Re: Add xml:lang attribute to multiples files at once

Post by cg.tw75 »

Hi,

I have tested both cases with the sample "flowers" project ( I added xml:lang="fr-fr" to the care.dita file to test the first case).
  • The "Find/Replace in Files" with the regular expression produces the same results: ="" is left.
  • I am sending a screenshot of the interface with the unrecognized xml:lang attribute. Maybe I am misunderstanding something.
  • The error message did not appear, but the root element of the map in the flowers project is <map title="Growing Flowers">, whereas for my ditamap it is <bookmap xml:lang="fr-fr" ditaarch:DITAArchVersion="1.2">. If I copy ditaarch:DITAArchVersion="1.2" into the flowers.ditamap root element , I get the error message (to be more specific, there are 2 error messages referencing this issue, one starting with org.xml.sax.SAXParseException).
As for the refactoring action changing the CDATA section, thanks for opening an issue. This should be a separate action, triggered by users and not done "behind their back".

Thank you for your help,
C.
radu_pisoi
Posts: 403
Joined: Thu Aug 21, 2003 11:36 am
Location: Craiova
Contact:

Re: Add xml:lang attribute to multiples files at once

Post by radu_pisoi »

cg.tw75 wrote:I am sending a screenshot of the interface with the unrecognized xml:lang attribute. Maybe I am misunderstanding something.
The problem is that you specify the 'xml:lang' value for the attribute local name. An attribute local name does not include the prefix part, only the local name could be specified.

So, you should modify the data as follow:
Local name: lang
Namespace: http://www.w3.org/XML/1998/namespace
cg.tw75 wrote:The error message did not appear, but the root element of the map in the flowers project is <map title="Growing Flowers">, whereas for my ditamap it is <bookmap xml:lang="fr-fr" ditaarch:DITAArchVersion="1.2">. If I copy ditaarch:DITAArchVersion="1.2" into the flowers.ditamap root element , I get the error message (to be more specific, there are 2 error messages referencing this issue, one starting with org.xml.sax.SAXParseException)
The 'ditaarch:DITAArchVersion' attribute is predefined in the DITA DTD files with a fixed value that cannot be changed in XML instances. So, I think you should remove it from your DITA files.
Radu Pisoi
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
cg.tw75
Posts: 14
Joined: Tue Jun 30, 2015 3:43 pm

Re: Add xml:lang attribute to multiples files at once

Post by cg.tw75 »

Great, it works with "lang" and the namespace. I had indeed misunderstood something, sorry about that.
I noticed something strange: in the sample "flowers" project, the "local name" field for "attribute" gave me a dropdown list of all valid options. In my project, there was no dropdown list.

One last point: I am still interested to know whether the "Find and replace in files" with the regular expression is supposed to work, as it was the original solution suggested in this topic.

Regards,
C.
radu_pisoi
Posts: 403
Joined: Thu Aug 21, 2003 11:36 am
Location: Craiova
Contact:

Re: Add xml:lang attribute to multiples files at once

Post by radu_pisoi »

cg.tw75 wrote:Great, it works with "lang" and the namespace. I had indeed misunderstood something, sorry about that.
I noticed something strange: in the sample "flowers" project, the "local name" field for "attribute" gave me a dropdown list of all valid options. In my project, there was no dropdown list.
The proposed values for the attribute local name are collected from the schema associated with the current edited file. Also, the proposed attributes depend on the current selected element.
Radu Pisoi
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Add xml:lang attribute to multiples files at once

Post by adrian »

cg.tw75 wrote:One last point: I am still interested to know whether the "Find and replace in files" with the regular expression is supposed to work, as it was the original solution suggested in this topic.
Yes, it's still supposed to work. Did you have trouble with it?

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
cg.tw75
Posts: 14
Joined: Tue Jun 30, 2015 3:43 pm

Re: Add xml:lang attribute to multiples files at once

Post by cg.tw75 »

Hi Adrian,
Here is the description of what I entered in the fields and of what happened as a result:
cg.tw75 wrote:xml:lang and fr-fr were removed, but ="" was left, making the xml invalid. Maybe I made another mistake? In the text to find box I had .*, in Restrict to XPath I had /*/@xml:lang, and I had nothing in Replace with. All options were selected in Enable XML search options.
Thank you for your help,
C.
cg.tw75
Posts: 14
Joined: Tue Jun 30, 2015 3:43 pm

Re: Add xml:lang attribute to multiples files at once

Post by cg.tw75 »

Hi Radu,
radu_pisoi wrote:The proposed values for the attribute local name are collected from the schema associated with the current edited file. Also, the proposed attributes depend on the current selected element.
In all cases, the "topic" element was selected.
  • The ditamap in the flowers sample project has <!DOCTYPE map PUBLIC "-//OASIS//DTD DITA Map//EN" "http://docs.oasis-open.org/dita/v1.1/OS/dtd/map.dtd">. The dropdown list shows all valid attributes for the "topic" element.
  • My ditamap had <!DOCTYPE bookmap PUBLIC "-//OASIS//DTD DITA BookMap//EN" "bookmap.dtd">, which is what Oxygen inserts automatically when you do New > Bookmap. The dropdown list stays empty.
  • If I replace "http://docs.oasis-open.org/dita/v1.1/OS/dtd/map.dtd" by "bookmap.dtd" in the flowers ditamap, the dropdown list is empty.
Regards,
C.
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Add xml:lang attribute to multiples files at once

Post by adrian »

Hi,
cg.tw75 wrote:All options were selected in Enable XML search options.
That's the problem, Enable XML search options acts as a filter, so it should be disabled.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
radu_pisoi
Posts: 403
Joined: Thu Aug 21, 2003 11:36 am
Location: Craiova
Contact:

Re: Add xml:lang attribute to multiples files at once

Post by radu_pisoi »

cg.tw75 wrote:In all cases, the "topic" element was selected.

The ditamap in the flowers sample project has <!DOCTYPE map PUBLIC "-//OASIS//DTD DITA Map//EN" "http://docs.oasis-open.org/dita/v1.1/OS/dtd/map.dtd">. The dropdown list shows all valid attributes for the "topic" element.
My ditamap had <!DOCTYPE bookmap PUBLIC "-//OASIS//DTD DITA BookMap//EN" "bookmap.dtd">, which is what Oxygen inserts automatically when you do New > Bookmap. The dropdown list stays empty.
If I replace "http://docs.oasis-open.org/dita/v1.1/OS/dtd/map.dtd" by "bookmap.dtd" in the flowers ditamap, the dropdown list is empty.
I was able to reproduce this issue. Therefore, I will register it in our issue tracker system.

Thank you for reporting it!
Radu Pisoi
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
cg.tw75
Posts: 14
Joined: Tue Jun 30, 2015 3:43 pm

Re: Add xml:lang attribute to multiples files at once

Post by cg.tw75 »

adrian wrote:That's the problem, Enable XML search options acts as a filter, so it should be disabled.
Indeed, it works when the option is deselected. I apologize for wasting your time, I should have tried deselecting it myself.

Thank you so much for your help!
C.
Phil Champ
Posts: 7
Joined: Wed Nov 26, 2014 3:22 pm

Re: Add xml:lang attribute to multiples files at once

Post by Phil Champ »

adrian wrote:
3. To add the xml:lang set on the root element of the files after the last attribute, use again the Find/Replace in Files and use the following field values:
Text to find: .*
Regular expression: Enabled
Restrict to XPath: /*/@*[last()]
Replace with: $0 xml:lang="en_US" (modify the value to the one you want to set)
These are helpful instructions, but one thing tripped me up for a while. xml:lang="en_US" is invalid XML according to http://www.ietf.org/rfc/bcp/bcp47.txt. It should be xml:lang="en-US".
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Add xml:lang attribute to multiples files at once

Post by adrian »

Hi,
Phil Champ wrote:It should be xml:lang="en-US".
Apologies for the confusion, that was a spelling error on my part.

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