Adding a specific attritbue/value pair for a selection of elements

Post here questions and problems related to editing and publishing DITA content.
Bas10R
Posts: 34
Joined: Thu May 25, 2023 4:08 pm

Adding a specific attritbue/value pair for a selection of elements

Post by Bas10R »

Hello,
Our documentation sometimes uses very long tables with some referential information. A simplified excerpt:

Code: Select all

                <table>
                    <tgroup>     
                        <thead>
                            <row>
                                <entry>Issue</entry>
                                <entry>Priority</entry>
                                <entry>Description</entry>
                                <entry>URL</entry>
                            </row>
                        </thead>
                        <tbody>
                            <row>
                                <entry>NUMBER1</entry>
                                <entry>9.8</entry>
                                <entry>TEXT
                                </entry>
                                <entry>XREF</entry>
                            </row>
                             <row>
                                <entry>NUMBER1</entry>
                                <entry>9</entry>
                                <entry>TEXT
                                </entry>
                                <entry>XREF</entry>
                            </row>
                             <row>
                                <entry>NUMBER1</entry>
                                <entry>8.5</entry>
                                <entry>TEXT
                                </entry>
                                <entry>XREF</entry>
                            </row>
                             <row>
                                <entry>NUMBER1</entry>
                                <entry>7.4</entry>
                                <entry>TEXT
                                </entry>
                                <entry>XREF</entry>
                            </row>
                             <row>
                                <entry>NUMBER1</entry>
                                <entry>7.2</entry>
                                <entry>TEXT
                                </entry>
                                <entry>XREF</entry>
                            </row>
                            
                            [...]
                            
I need to add a specific attribute/value pair to a custom selection (based on the number in the "Priority" column) of <entry> elements in the second column.
Is there any efficient way of doing that? The tables can have dozens of rows, so I need a way to apply such bulk changes.

I'd rather avoid using Find/Replace regex in Text mode to match specific entries and add an attribute for those entries. For example, to match entries with element value between "5.5" and "6.5":

Code: Select all

Find: <entry>(5\.([5-9]|[0-9]\d*)|6\.[0-5])</entry>
Replace: <entry outputclass="priorityX">$1</entry>
The reason for that is I'm looking for something that's built into Oxygen to enable authors not versed in regex to make such changes.

Kind regards,
Konrad
Radu
Posts: 9059
Joined: Fri Jul 09, 2004 5:18 pm

Re: Adding a specific attritbue/value pair for a selection of elements

Post by Radu »

Hi,
Can you give us more details about your setup?
Are you working with Oxygen desktop or with our in-browser WebAuthor component?
Are you working with the XML content in the Author visual editing mode or in the Text mode?
What type of XML vocabulary are you editing?
Have you implemented custom Oxygen Author actions until now?
Are you more familiar with using Java, Javascript or XSLT to create custom actions?
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Bas10R
Posts: 34
Joined: Thu May 25, 2023 4:08 pm

Re: Adding a specific attritbue/value pair for a selection of elements

Post by Bas10R »

Hi Radu,

- Oxygen XML Editor 25.0.
- I'd prefer a solution in the Author mode (see the info about regex in my original post).
- We're using a standard out-of-the-box DTD/XML setup provided by Oxygen, a very basic startup project.
- Only played with some really basic custom actions as part of one of the webinars you guys did.
- Not familiar with Java, Javascript, or XSLT.

As I mentioned, I'm looking for something basic. If it's not available in Oxygen's GUI, I can deal with that through Find/Replace with regex, I just though it's something pretty basic, so I must have missed something :)
The following topic describes a similar scenario: feature-request/topic15283.html
chrispitude
Posts: 907
Joined: Thu May 02, 2019 2:32 pm

Re: Adding a specific attritbue/value pair for a selection of elements

Post by chrispitude »

Hi Bas10R,

If you are using Oxygen on the desktop, you can probably use an XSLT refactoring operation, and I can try to write it for you.

I saw your sample table structure, but can you also please provide a "before" and "after" so I can better understand the type of operation you need?

- Chris
Bas10R
Posts: 34
Joined: Thu May 25, 2023 4:08 pm

Re: Adding a specific attritbue/value pair for a selection of elements

Post by Bas10R »

Hi Chris,

Before:

Code: Select all

<table>
                    <tgroup>     
                        <thead>
                            <row>
                                <entry>Issue</entry>
                                <entry>Priority</entry>
                                <entry>Description</entry>
                                <entry>URL</entry>
                            </row>
                        </thead>
                        <tbody>
                            <row>
                                <entry>ISSUE-123</entry>
                                <entry>9.8</entry>
                                <entry>TEXT</entry>
                                <entry>XREF</entry>
                            </row>
                             <row>
                                <entry>ISSUE-456</entry>
                                <entry>9</entry>
                                <entry>TEXT</entry>
                                <entry>XREF</entry>
                            </row>
                             <row>
                                <entry>ISSUE-789</entry>
                                <entry>8.5</entry>
                                <entry>TEXT</entry>
                                <entry>XREF</entry>
                            </row>
                             <row>
                                <entry>ISSUE-101</entry>
                                <entry>7.4</entry>
                                <entry>TEXT</entry>
                                <entry>XREF</entry>
                            </row>
                             <row>
                                <entry>ISSUE-102</entry>
                                <entry>7.2</entry>
                                <entry>TEXT</entry>
                                <entry>XREF</entry>
                            </row>
                            
                            [...]
After:

Code: Select all

<table>
                    <tgroup>     
                        <thead>
                            <row>
                                <entry>Issue</entry>
                                <entry>Priority</entry>
                                <entry>Description</entry>
                                <entry>URL</entry>
                            </row>
                        </thead>
                        <tbody>
                            <row>
                                <entry>ISSUE-123</entry>
                                <entry outputclass="priority1">9.8</entry>
                                <entry>TEXT</entry>
                                <entry>XREF</entry>
                            </row>
                             <row>
                                <entry>ISSUE-456</entry>
                                <entry outputclass="priority1">9</entry>
                                <entry>TEXT</entry>
                                <entry>XREF</entry>
                            </row>
                             <row>
                                <entry>ISSUE-789</entry>
                                <entry outputclass="priority1">8.5</entry>
                                <entry>TEXT</entry>
                                <entry>XREF</entry>
                            </row>
                             <row>
                                <entry>ISSUE-101</entry>
                                <entry>7.4</entry>
                                <entry>TEXT</entry>
                                <entry>XREF</entry>
                            </row>
                             <row>
                                <entry>ISSUE-102</entry>
                                <entry>7.2</entry>
                                <entry>TEXT</entry>
                                <entry>XREF</entry>
                            </row>
                            
                            [...]
So it's about adding specific attribute/value pairs (outputclass="priority1" is just one example) to specific <entry> elements (the ones that correspond to the "Priority" column). For example, adding outputclass="priority1" to all "Priority" <entry> elements with a value between 7.5 and 10.

Kind regards,
Konrad
chrispitude
Posts: 907
Joined: Thu May 02, 2019 2:32 pm

Re: Adding a specific attritbue/value pair for a selection of elements

Post by chrispitude »

Hi Konrad,

Here's a testcase as a starting point:

oxygen_apply_outputclass_to_table.zip
(2.86 KiB) Downloaded 91 times

It provides the following refactoring operation that can be run on maps and topics:

image.png
image.png (10.85 KiB) Viewed 645 times

For any "Priority" columns that are the second column in the table, cells with values between 7.5 and 10 (inclusive) will have the attribute added:

image.png
image.png (72.87 KiB) Viewed 645 times

If an @outputclass attribute (of any value) exists on a cell that does not meet this criteria, it is removed. This behavior can be made smarter as needed.

The pertinent stylesheet template is as follows:

Code: Select all

    <xsl:template match="table[$update-priorities]/tgroup[thead[row[entry[2] eq 'Priority']]]/tbody/row/entry[2]">
        <xsl:variable name="result" as="element()">
            <xsl:next-match/>  <!-- apply other templates first -->
        </xsl:variable>
        <xsl:copy select="$result">
            <xsl:choose>
                <xsl:when test="not(. castable as xs:double)">
                    <!-- value is not numeric -->
                </xsl:when>
                <xsl:when test="(. >= 7.5) and (. &lt;= 10.0)">
                    <!-- priority is between 7.5 and 10 -->
                    <xsl:attribute name="outputclass">priority1</xsl:attribute>
                </xsl:when>
            </xsl:choose>
            <!-- copy all attributes and content except original @outputclass attribute -->
            <xsl:sequence select="(@* except @outputclass)|node()"/>
        </xsl:copy>
    </xsl:template>
Let me know if you have any questions about how it works or how to modify it!

- Chris
Bas10R
Posts: 34
Joined: Thu May 25, 2023 4:08 pm

Re: Adding a specific attritbue/value pair for a selection of elements

Post by Bas10R »

Works like a charm, Chris. I think I can handle creating rules for different ranges and applying this custom XML refactoring action to other projects.

Thank you for your help!

Kind regards,
Konrad
chrispitude
Posts: 907
Joined: Thu May 02, 2019 2:32 pm

Re: Adding a specific attritbue/value pair for a selection of elements

Post by chrispitude »

Hi Konrad,

I'm glad it worked for you! If you have questions about how to implement additional functionality, feel free to ask here.

Note that I used <xsl:next-match/> in the element template, which calls the next template that would have been called if the current template had not matched, then processes that result. I could have skipped that and modified the current element directly instead. But by processing the results of <xsl:next-match/>, the template will "play nice with others" when multiple templates apply to the same element.

If you are curious to learn more about how refactoring can be used to process DITA content, there are some Oxygen XML Blog posts in DITA > Other > Refactoring.
Post Reply