Page 1 of 1

Default rowsep/colsep/frame in transformation if not set

Posted: Thu Jul 21, 2022 4:42 pm
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

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

Posted: Thu Jul 21, 2022 5:59 pm
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.

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

Posted: Fri Jul 22, 2022 11:18 am
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

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

Posted: Fri Jul 22, 2022 2:25 pm
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?

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

Posted: Mon Jul 25, 2022 12:54 pm
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>

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

Posted: Mon Jul 25, 2022 8:22 pm
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.

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

Posted: Tue Jul 26, 2022 1:33 pm
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:

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

Posted: Tue Jul 26, 2022 2:16 pm
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.

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

Posted: Tue Jul 26, 2022 3:39 pm
by Heidi
Oh this is great, thank you so much!
This will help a lot next time. Saved as favorit :D