Protect An Element / Applying Value Using XSLT

Here should go questions about transforming XML with XSLT and FOP.
john_m
Posts: 32

Protect An Element / Applying Value Using XSLT

Mon Apr 10, 2017 9:39 pm

Hello,

I am working on a desired feature for an XML document that will be authored using Oxygen XML Web Author.

The XML document has <title> elements - and each <title> has an optional hardTitle attribute. If hardTitle is populated, the element content should reflect the attribute value. If hardTitle is not populated, the title can be whatever the author wants.

Example:

Code: Select all

<chapter>
    <title hardTitle="Scope of Effort">Scope of Effort</title>
</chapter>
<chapter>
    <title>Any Title</title>
</chapter>


Currently, I have an XSL file/transformation scenario that will apply the value of hardTitle to the element value. However, the update has to be run manually (instead of an on-change action) - and it requires a new file be created to hold the changes.

Code: Select all

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
    </xsl:template>
   
    <xsl:template match="title[@hardTitle]">
        <xsl:copy>
            <!-- supposes that <title> elements will not have child elements -->
            <xsl:apply-templates select="@*" />
            <xsl:value-of select="@hardTitle" />
        </xsl:copy>
    </xsl:template>
   
</xsl:stylesheet>


Ideally, I would like any <title> with a populated hardTitle attribute to act as if it were protected (the user's can't edit it even if they try). I was trying to implement it so that:
1.) On element exit, the transform would run
2.) The transformed document (with the correct title) would reload in the editor window

This would have the effect of not allowing users to change the title.

Of course, I am open to any other ways of implementing this desired behavior. Any suggestions would be greatly appreciated!

Thanks!
John
cristi_talau
Posts: 123

Re: Protect An Element / Applying Value Using XSLT

Tue Apr 11, 2017 9:41 am

Hello,

You can create a better user experience by tweaking the CSS used for rendering. For example:

Code: Select all

// if the title element has a "hardTitle" attribute
title[hardTitle] {
    -oxy-editable: false;               // The content of the title will not be editable
    visibility: -oxy-collapse-text;   // The text content of the title will not be rendered
    content: attr(hardTitle);          // Instead, render the value of the "hardTitle" attribute
}


You can also register an AuthorDocumentFilter [1] that will update the content of the title whenever the "hardTitle" attribute is changed. Or even more, reject any updates on the "hardTitle" attribute.

Best,
Cristian

[1] https://www.oxygenxml.com/InstData/Edit ... ilter.html
john_m
Posts: 32

Re: Protect An Element / Applying Value Using XSLT

Tue Apr 11, 2017 6:04 pm

Hi Christian!

Thank you so much for the help. Your css suggestion providee the exact desired behavior.

Now I have a follow-up question though. When the user tries to modify a <title> that has a hardTitle, they get the following error message:
Insert text failedThe selection has an endpoint inside an invisible range: 646

I would like to change the error message to something along the lines of:
[hardTitleValue] is a required title or, if it simplifies things, This title is required and cannot be changed.

Is there a way to customize the message? I found the following forum entry - but I'm not using reference revolvers.
post36611.html?hilit=customize%20error%20message#p36611

Any suggestions would be greatly appreciated!

Thanks!
John
cristi_talau
Posts: 123

Re: Protect An Element / Applying Value Using XSLT

Wed Apr 12, 2017 8:49 am

Hello,

You can try the AuthorDocumentFilter Java extension that I mentioned in a previous post. That way you can block the character insertion and provide a custom error message using AuthorAccess#showErrorMessage() [1].

Best,
Cristian

[1] https://www.oxygenxml.com/InstData/Edit ... ccess.html

Return to “XSLT and FOP”

Who is online

Users browsing this forum: No registered users and 0 guests