Default rowsep/colsep/frame in transformation if not set

Post here questions and problems related to editing and publishing DITA content.
Heidi
Posts: 9
Joined: Thu Jul 21, 2022 4:07 pm

Default rowsep/colsep/frame in transformation if not set

Post by Heidi »

Hi,
I want to define the default setting for frame, rowsep and colsep to true, when nothing is set. We use a merged2mergedExtention.xsl as described here: https://www.oxygenxml.com/doc/versions/ ... plate.html
So I thought it would be great to add the default settings there, but I'm totally stuck.
I managed to insert rowsep and colsep when both is missing (I assume that it is only a misstake when both aren't set, don't know if this makes sense):

Code: Select all

    <xsl:template match="table[(not(@colsep)) and (not(@rowsep))]">
        <table>
            <xsl:attribute name="colsep">1</xsl:attribute>
            <xsl:attribute name="rowsep">1</xsl:attribute>
            <xsl:apply-templates select="@*|node()"/>
        </table>
    </xsl:template>
But when I add the frame with an <xsl:if>, it doesn't work:

Code: Select all

    <xsl:template match="table[((not(@colsep)) and (not(@rowsep))) or (not(@frame))]">
        <xsl:if table="(not(@colsep)) and (not(@rowsep))">
            <table>
                <xsl:attribute name="colsep">1</xsl:attribute>
                <xsl:attribute name="rowsep">1</xsl:attribute>
                <xsl:apply-templates select="@*|node()"/>
            </table>
        </xsl:if>
        <xsl:if table="not(@frame)">
            <table>
                <xsl:attribute name="frame">all</xsl:attribute>
                <xsl:apply-templates select="@*|node()"/>
            </table>
        </xsl:if>
    </xsl:template>
Can anybody help me out and tell me where my mistake is?
Thank you!
Heidi
chrispitude
Posts: 907
Joined: Thu May 02, 2019 2:32 pm

Re: Default rowsep/colsep/frame in transformation if not set

Post by chrispitude »

Hi Heidi,

I have a similar request here, but for authoring (being able to specify the defaults for new table insertions):

Have parameters for new tables default to <unspecified>

I thought I would point it out in case it was also interesting to you.
Heidi
Posts: 9
Joined: Thu Jul 21, 2022 4:07 pm

Re: Default rowsep/colsep/frame in transformation if not set

Post by Heidi »

Dear chrispitude,

thank you for your reply. Your post is absolutely relevant for the future and I'm with you, that everything should stay empty except you want something different from your standard. I wonder why my default setting is different from yours (frame=all, rowsep and colsep undefined). Maybe it depends on what oxygen version you first installed. That makes everything even worse. We have at least two different default settings in the company. It should be able to set the default settings for new tables in the settings.
But my problem is one step earlier: I have something wrong in my code to set the default (for not defined attributes) and I cant't get the transformation to run. :?

Best regards
Heidi
Last edited by Heidi on Fri Jul 22, 2022 11:55 am, edited 2 times in total.
chrispitude
Posts: 907
Joined: Thu May 02, 2019 2:32 pm

Re: Default rowsep/colsep/frame in transformation if not set

Post by chrispitude »

Hi Heidi,

I don't know if this would help you, but I could provide a refactoring operation that modifies your existing DITA source to use a certain separator/frame convention. That's not the same as modifying it during transformation, but perhaps the better solution is to fix the source anyway.

Would that be useful?
Heidi
Posts: 9
Joined: Thu Jul 21, 2022 4:07 pm

Re: Default rowsep/colsep/frame in transformation if not set

Post by Heidi »

Hi chrispitude,

thanks a lot for the offer, but I prefer to customize it in the transformation.
The error ist found and we deceided to split rowsep and colsep too. Here is the correct code for the merged2mergedExtention.xsl, if somebody needs it:

Code: Select all

<xsl:template match="table[((not(@colsep)) or (not(@rowsep))) or (not(@frame))]">
        <xsl:if test="not(@colsep)">
            <table>
                <xsl:attribute name="colsep">1</xsl:attribute>
                <xsl:apply-templates select="@*|node()"/>
            </table>
        </xsl:if>
        <xsl:if test="not(@rowsep)">
            <table>
                <xsl:attribute name="rowsep">1</xsl:attribute>
                <xsl:apply-templates select="@*|node()"/>
            </table>
        </xsl:if>
        <xsl:if test="not(@frame)">
            <table>
                <xsl:attribute name="frame">all</xsl:attribute>
                <xsl:apply-templates select="@*|node()"/>
            </table>
        </xsl:if>
    </xsl:template>
chrispitude
Posts: 907
Joined: Thu May 02, 2019 2:32 pm

Re: Default rowsep/colsep/frame in transformation if not set

Post by chrispitude »

Hi Heidi,

I think that might result in multiple <table> elements if multiple attributes are missing, with each table produced having only one of the missing attributes.

You could also consider the following approach:

Code: Select all

    <xsl:template match="table">
        <xsl:copy>
            <xsl:attribute name="colsep" select="'1'"/>  <!-- default -->
            <xsl:attribute name="rowsep" select="'1'"/>  <!-- default -->
            <xsl:attribute name="frame" select="'all'"/>  <!-- default -->
            <xsl:apply-templates select="@*|node()"/>  <!-- overrides defaults if specified -->
        </xsl:copy>
    </xsl:template>
which takes advantage of the fact that if the same attribute is defined twice for the same element, XSLT keeps the last value. So in this case, we set all our desired default attributes, then <xsl:apply-templates> replaces any of them as needed.
Heidi
Posts: 9
Joined: Thu Jul 21, 2022 4:07 pm

Re: Default rowsep/colsep/frame in transformation if not set

Post by Heidi »

Hi chrispitude,
I had a stupid test case so I didn't saw the duplication of the tables :shock:
Thank you so much for helping me out. Now it works perfectly and its very elegant :wink:
chrispitude
Posts: 907
Joined: Thu May 02, 2019 2:32 pm

Re: Default rowsep/colsep/frame in transformation if not set

Post by chrispitude »

Hi Heidi,

I made similar mistakes when I was starting out with XSLT. I am better now, but I still have a lot to learn. :)

Here is a good place to experiment and learn:

https://xsltfiddle.liberty-development.net/

Delete everything after the <xsl:mode> (you don't want HTML settings or the default templates), then put some DITA content in the top-left side and experiment. The output on the bottom-left will update as you make changes to the XSLT code. It's a great way to experiment, learn, and get the syntax and logic right.
Heidi
Posts: 9
Joined: Thu Jul 21, 2022 4:07 pm

Re: Default rowsep/colsep/frame in transformation if not set

Post by Heidi »

Oh this is great, thank you so much!
This will help a lot next time. Saved as favorit :D
Post Reply