Search code examples
xsltditadita-ot

How can I use processing instruction value in a different XSL template?


I am unable to use processing-instruction attribute values in a different template that is already used to add topic details in the header of the output file.

<task xml:lang="en-us" id="_01FDEB11">
    <?ASTDOCREVINFO __docVerName="1.6" __docVerDesc="Description goes here" __docVerUser="Leroy" __docVerDate="Sep 25, 2017 10:44:44 AM"?>

I've created a template to pull values from a processing instruction but the variables don't keep the values in a different template.

<xsl:template match="processing-instruction('ASTDOCREVINFO')">

Version: <xsl:value-of select="substring-before(substring-after(., '__docVerName=&quot;'), '&quot;')"/> 
Date: <xsl:value-of select="substring-before(substring-after(., '__docVerDate=&quot;'), '&quot;')"/>

<xsl:variable name="astVersion" select="substring-before(substring-after(., '__docVerName=&quot;'), '&quot;')"/>
<xsl:variable name="astDate" select="substring-before(substring-after(., '__docVerDate=&quot;'), '&quot;')"/>

Variable Version: <xsl:value-of select="$astVersion"/>
Variable Date: <xsl:value-of select="$astDate"/>

</xsl:template>

I'm unable to get this to work in a different template already used to pull topic info into the header of the output file.

    <xsl:template
        match="*[contains(@class, ' topic/topic ')][not(parent::*[contains(@class, ' topic/topic ')])]/*[contains(@class, ' topic/title ')]">

How can I add "processing-instruction('ASTDOCREVINFO')" to this template match?


Solution

  • You cannot pass information from one template match to another as XSLT is side effect free but in your second template you can use XPath to match the processing instruction which is a child of the root element. Something like:

    <xsl:template
        match="*[contains(@class, ' topic/topic ')][not(parent::*[contains(@class, ' topic/topic ')])]/*[contains(@class, ' topic/title ')]">
        <!-- /* => means the root element of the XML document -->
        <xsl:variable name="astoriaPI" select="/*/processing-instruction('ASTDOCREVINFO')"/>
        <xsl:variable name="astVersion" select="substring-before(substring-after($astoriaPI, '__docVerName=&quot;'), '&quot;')"/>
        <xsl:variable name="astDate" select="substring-before(substring-after($astoriaPI, '__docVerDate=&quot;'), '&quot;')"/>
    </xsl:template>