Create an ID based on existing term/string

Having trouble installing Oxygen? Got a bug to report? Post it all here.
hugi
Posts: 2
Joined: Tue Sep 08, 2020 12:05 pm

Create an ID based on existing term/string

Post by hugi »

Hi,

Disclaimer: I am still a noob in the Oxygen XML Editor world!
I am using Oxygen XML Editor for research purpose, currently working on application projects to learn how to better use it.
I am trying to create IDs based on existing terms/strings for selected elements in an automated way, but can't seem to find a solution (if ever there is one).

Here's the context:
I work on DITA migration projects and always re-use content with conrefs, etc. attributes. For this purpose, I create a dita file which contains a table such as:
image.png
image.png (13.52 KiB) Viewed 1482 times
This kind of table allows me to store and sort all the re-used content, but also to create glossaries for other applications, so this display is kind of important.
I then work row by row in the table (Author view): I assign a tag (uicontrol, ph, wintitle...) to a term that appears in the "Term" column, and add an ID to it. Said ID is supposed to be the same as the one that appears in the "ID" column on the same line.
Here's how a line looks like after this step in the Text view:

Code: Select all

<row>
        <entry><uicontrol id="adapt_paper_size">Adapter à la taille du papier</uicontrol></entry>
        <entry>adapt_paper_size</entry>
        <entry>MAC</entry>
</row>
At the moment, I do all this manually, and I am searching for a way to semi-automatically do this.

I know I can automatically generate an ID when a new pair of tags is created, but I don't know if it is possible to customise the ID generation pattern so that the ID is drawn from that "ID" column? In other words, would it be possible to assign to the string in the first entry of the row an ID based on the string of the second entry of the row?
If yes, all I'd have to do is assign the correct tags to each of the terms, and the rest of the process would be automatically executed.

Don't hesitate is something is not clear!

Thanks,

Hugo
Hugo Sylvestre
Translation - Technical writing
Radu
Posts: 9049
Joined: Fri Jul 09, 2004 5:18 pm

Re: Create an ID based on existing term/string

Post by Radu »

Hi Hugo,

The default ID generation is based on a simple pattern designed to produce an unique ID value so it cannot express the complexity that you need.
Oxygen does have Java API, it has a free SDK which would allow you to write Java extensions which would take control over the generated ID attribute values:

https://www.oxygenxml.com/oxygen_sdk.html

so if you have a Java developer willing to help this might be an option.
There are also other alternatives which might be easier to achieve:

1)
You can add custom Author actions to the DITA editing framework, for example you could create a custom action named "Update IDs" which would call Javascript code working with our APIs to change attribute values. There are a bunch of examples here:

https://github.com/oxygenxml/javascript ... operations

like for example this operation sets a certain attribute value on multiple elements:

https://github.com/oxygenxml/javascript ... e-elements
So a custom ""Update IDs" action added for example to the main menu could iterate the entire topic contents and update all IDs on elements to match the ID column value.

2)
Oxygen has support for custom XML refactoring actions based on XSLT:

https://www.oxygenxml.com/doc/versions/ ... tools.html

We have a bunch of examples here:
https://github.com/oxygenxml/dita-refactoring-examples

A sample XSLT processing an entire topic and generating IDs for elements based on the text value from the ID cell might look something like:

Code: Select all

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <!-- Copy everything as it is -->
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
    
    <!-- Match the elements inside the first entry in any row -->
    <xsl:template match="row/entry[1]/*">
        <xsl:copy>
            <!-- Copy all attributes except the ID -->
            <xsl:apply-templates select="@* except @id"/>
            <!-- Generate a new ID attribute based on the text from the next entry -->
            <xsl:attribute name="id" select="(parent::entry/following-sibling::entry)[1]/text()"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
hugi
Posts: 2
Joined: Tue Sep 08, 2020 12:05 pm

Re: Create an ID based on existing term/string

Post by hugi »

Hi Radu,

First of all, thank you for all your explanations and information, that's great help.

For the moment, I found a workaround with the custom XML refactoring action solution. I created an XSL file based on the code your provided and a refactoring operation descriptor file linked to the XSL file and it works great.
It's already a big improvement since all I have to do on this step with this solution is put all the elements of the first column between tags and launch the XML refactoring action.

I'll also explore the other possible solutions you provided me to see what would be the best one on the long term.

Once again, thank you so much for your amazing help!

Hugo
Hugo Sylvestre
Translation - Technical writing
Radu
Posts: 9049
Joined: Fri Jul 09, 2004 5:18 pm

Re: Create an ID based on existing term/string

Post by Radu »

Hi Hugo,

Great, thinking in terms of implemnentation effort versus benefits I also think the XSLT refactoring action is the best choice for you right now.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply