Page 1 of 1

XSLT obtain a value of revised element in PDF

Posted: Thu Jul 13, 2023 3:01 am
by Tinmen
I am trying to pull a value from the critdates element, specifically <revised>.

The XML is:

Code: Select all

<bookmeta>        
        <author></author>
        <critdates>
            <created date="2023"/>
            <revised modified="2024"></revised>
        </critdates>
        <bookid>
            <bookpartno>4598638</bookpartno>
            <booknumber>6.2.0</booknumber>
        </bookid>        
    </bookmeta>
When I use:

Code: Select all

<fo:block>
            <xsl:if test="//*[contains(@class, ' bookmap/bookmeta ')][1]"> 
                <xsl:value-of select="//*[contains(@class, ' bookmap/bookpartno ')]/text()"/></xsl:if>   
 </fo:block>
the bookpartno data is printed in the PDF.
When I run a similar code using the revised element:

Code: Select all

fo:block>
            <xsl:if test="//*[contains(@class, ' bookmap/bookmeta ')][1]"> 
            <xsl:value-of select="//*[contains(@class, ' critdates/revised/@modified ')]/text()"/></xsl:if>   
        </fo:block>
nothing is returned.
I have tried various combinations of the path, such as revised/@modified, bookmap/revised/@modified, etc., and I have substituted "attribute" for "class" in the above code, but that hasn't helped.

Any ideas as to why this doesn't work for the revised element?

Re: XSLT obtain a value of revised element in PDF

Posted: Thu Jul 13, 2023 6:56 am
by Radu
Hi,
I think based on your intention your XPath expression should look like this:

Code: Select all

<xsl:value-of select="//*[contains(@class, ' topic/critdates ')]/*[contains(@class, ' topic/revised ')]/@modified"/>
or this if you are not using specializations with other names for the element:

Code: Select all

<xsl:value-of select="//critdates/revised/@modified"/>
Regards,
Radu

Re: XSLT obtain a value of revised element in PDF

Posted: Thu Jul 13, 2023 5:36 pm
by Tinmen
Thank you, Radu!
Your help is very much appreciated.

Code: Select all

<xsl:value-of select="//*[contains(@class, ' topic/critdates ')]/*[contains(@class, ' topic/revised ')]/@modified"/>
works, to a point.
However, the PDF prints 2024 2024 2024 2024 2024 2024 instead of a single instance of 2024.
I have found this happens when I use topic/ in the @class statements. I haven't found a resolution.

I also tried:

Code: Select all

<xsl:value-of select="//critdates/revised/@modified"/>
which also renders:
2024 2024 2024 2024 2024 2024

So,

Code: Select all

<xsl:value-of select="//bookmeta/critdates/revised/@modified"/>
works and prints only one instance of the date in the PDF.

Thanks again, Radu, for your help.

Regards
Tinmen

Re: XSLT obtain a value of revised element in PDF

Posted: Fri Jul 14, 2023 6:49 am
by Radu
Hi Tinmen,
Whenever you get a sequence of output values you can always do stuff like this:

Code: Select all

<xsl:value-of select="(//critdates/revised/@modified)[1]"/>
to take only the first returned result.
Usually when you have problems creating XPath expressions you need to understand on what XML document they get applied. Your XSLT is applied on an XML document which is the bookmap with all topicrefs expanded in it. If you use something like:

Code: Select all

<xsl:message><xsl:copy-of select="/*"/></xsl:message>
you should get in the ANT console output the entire merged XML document printed. Then you can copy it from there, create a new XML document in Oxygen and try to run xpath expressions on it using the Oxygen XPath toolbar for example.
Regards,
Radu

Re: XSLT obtain a value of revised element in PDF

Posted: Fri Jul 14, 2023 7:20 pm
by Tinmen
Hi Radu:

Makes sense. I had originally tried the [1], but my syntax must have been wrong. I used your code, and it works perfectly. I did realize the multiple instances of the value matched the page count in my document. I couldn't find a way to fix it initially.

Thanks again for your tremendous support.

Regards

Tinment