Add revised and modified date information within topic body

Post here questions and problems related to editing and publishing DITA content.
gbv34
Posts: 81
Joined: Thu Jan 20, 2022 12:36 pm

Add revised and modified date information within topic body

Post by gbv34 »

Hello, everyone!
I want to insert right after the main topic title the revised and modified dates values accessible from the critdates element (btw, it's for an HTML5 transtype and I have a dedicated plugin).
I want to apply my xslt to an existing template (based on the class topic/body), but I noticed two inconsistencies:
1. the created date attribute is applied for every topic even when there is no critdates declaration in it
2. the @date attribute value that is inserted in every topic is repeated whereas several topics have different dates declared...
image.png
image.png (16.11 KiB) Viewed 219 times
image.png
image.png (18.13 KiB) Viewed 219 times
For now, my xslt uses this declaration:

Code: Select all

 
 <xsl:template match="*[contains(@class, ' topic/body ')]" name="topic.body">
        <!-- Here is my declaration for critdates -->
        <div>
            <xsl:choose>
                <xsl:when test="*/critdates/created/@date &gt; 0">
                    Date of creation: 
                    <xsl:value-of select="/concept/prolog/critdates/created/@date"/>
                </xsl:when>
                <xsl:otherwise>Error:
                    <xsl:value-of select="/concept/prolog/critdates/created/@date"/>
                </xsl:otherwise>
            </xsl:choose>
        </div>
        <div>
            <xsl:call-template name="commonattributes"/>
            <xsl:call-template name="setidaname"/>
            <xsl:apply-templates select="*[contains(@class, ' ditaot-d/ditaval-startprop ')]" mode="out-of-line"/>
            <xsl:apply-templates select="preceding-sibling::*[contains(@class, ' topic/abstract ')]" mode="outofline"/>
            <xsl:apply-templates select="preceding-sibling::*[contains(@class, ' topic/shortdesc ')]" mode="outofline"/>
            <xsl:apply-templates select="following-sibling::*[contains(@class, ' topic/related-links ')]" mode="prereqs"/>
            <xsl:apply-templates/>
            <xsl:apply-templates select="*[contains(@class, ' ditaot-d/ditaval-endprop ')]" mode="out-of-line"/>
        </div>
    </xsl:template>
Would you have any idea of the problem?
------
Gaspard
Radu
Posts: 8283
Joined: Fri Jul 09, 2004 5:18 pm

Re: Add revised and modified date information within topic body

Post by Radu »

Hi,
I did something similar for the Oxygen XML Blog, display the Author and created dates in the HTML output and then use them to create a what's new list and an RSS feed:

https://blog.oxygenxml.com/topics/recen ... _list.html
https://blog.oxygenxml.com/topics/rss_f ... ation.html

I did that as part as a Webhelp publishing template but the XSLT code should be usable in a plain HTML plugin:
https://github.com/oxygenxml/blog/blob/ ... prolog.xsl

About your questions:
1. the created date attribute is applied for every topic even when there is no critdates declaration in it
How about if you add an xsl:if around that <div> element? Something like:

Code: Select all

<xsl:if test="*/critdates">
    <div>
	<xsl:choose>
    ....
    </div>
    </xsl:if>
2. the @date attribute value that is inserted in every topic is repeated whereas several topics have different dates declared...
You mean in the same topic file you have several inner topics, concepts, right?
An XPath like this "/concept/prolog/critdates/created/@date" is an absolute xpath, it matches the root "/concept" element and navigates down on it.
Your template matches a "body" element, so you should focus on its previous "prolog" sibling.
So maybe something like:

Code: Select all

<xsl:if test="preceding-sibling::prolog/critdates">
  <div>
            <xsl:choose>
                <xsl:when test="preceding-sibling::prolog/critdates/created/@date &gt; 0">
                    Date of creation: 
                    <xsl:value-of select="preceding-sibling::prolog/critdates/created/@date"/>
                </xsl:when>
                <xsl:otherwise>Error:
                    <xsl:value-of select="preceding-sibling::prolog/critdates/created/@date"/>
                </xsl:otherwise>
            </xsl:choose>
        </div>
</xsl:if>
        
Also this condition preceding-sibling::prolog/critdates/created/@date &gt; 0 I do not consider it appropriate, the date is a string with the pattern "YYYY-MM-DD": https://www.oxygenxml.com/dita/1.3/spec ... dates.html
Comparing a string with an integer value (0) does not make sense.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Radu
Posts: 8283
Joined: Fri Jul 09, 2004 5:18 pm

Re: Add revised and modified date information within topic body

Post by Radu »

One more thing, you can also do stuff like:

Code: Select all

 
 <xsl:template match="*[contains(@class, ' topic/body ')]" name="topic.body">
        <!-- Here is my declaration for critdates -->
       <div>
         .....
      </div>
       <xsl:next-match/>
    </xsl:template>
That <xsl:next-match/> will call the base xsl:template which matches the same element and thus you avoid copying from the base template this part of content:

Code: Select all

        <div>
            <xsl:call-template name="commonattributes"/>
            <xsl:call-template name="setidaname"/>
            <xsl:apply-templates select="*[contains(@class, ' ditaot-d/ditaval-startprop ')]" mode="out-of-line"/>
            <xsl:apply-templates select="preceding-sibling::*[contains(@class, ' topic/abstract ')]" mode="outofline"/>
            <xsl:apply-templates select="preceding-sibling::*[contains(@class, ' topic/shortdesc ')]" mode="outofline"/>
            <xsl:apply-templates select="following-sibling::*[contains(@class, ' topic/related-links ')]" mode="prereqs"/>
            <xsl:apply-templates/>
            <xsl:apply-templates select="*[contains(@class, ' ditaot-d/ditaval-endprop ')]" mode="out-of-line"/>
        </div>
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
gbv34
Posts: 81
Joined: Thu Jan 20, 2022 12:36 pm

Re: Add revised and modified date information within topic body

Post by gbv34 »

Hello, Radu!

Thanks a lot! That helped a lot, indeed. More particularly using the xsl:next-match to avoid copy-pasting all the previous declarations.
I also figured out why the date remained the same whatever the topic is: the map included a critdates element in its topicmeta. Therefore, the critdates declared at the map level inherits at the topic level. Removing it and declaring individual values for topics displayed a different date for each one.

I guess I have to set a condition to check if there's already a value declared in the topicmeta.
------
Gaspard
Post Reply