[xsl] Applying template with a variable

Subject: [xsl] Applying template with a variable
From: Harry Liljeström <harry.liljestrom@xxxxxxxxxxxxx>
Date: Wed, 17 Jan 2007 12:49:05 +0200 (EET)
Hi,

Is it possible in apply of template do variable selection in XSLT 1.0?

1. The variable authobj consists of authentication-object node.
2. <xsl:apply-templates select="$authobj"/> should launch the <xsl:template match="authentication-object[type = 'plain-file']">.

Note: The template with match //authentication/object is in different context, than the authentication-object:s!


<xsl:variable name="locauthobjs" select="/config/authentication-objects/authentication-object"/> <xsl:variable name="globauthobjs" select="document('../etc/policyglobal.xml')/config/authentication-objects/authentication-object"/>

<xsl:template match="//authentication/object">
  <xsl:variable name="authobj">
     <xsl:call-template name="authobjbyname">
        <xsl:with-param name="name" select="."/>
     </xsl:call-template>
  </xsl:variable>
  <xsl:apply-templates select="$authobj"/><!-- [1.] -->
</xsl:template>

<xsl:template match="authentication-object[type = 'plain-file']"><!-- [2.] -->
<xsl:variable name="keyfiles">
<xsl:call-template name="key_file">
<xsl:with-param name="path" select="path"/>
</xsl:call-template>
</xsl:variable> <exteranal-key type="{'software'}" init-info="{$keyfiles}"/>
</xsl:template>


<xsl:template name="authobjbyname">
  <xsl:param name="name"/>
     <xsl:if test="$name !=''">
        <xsl:choose>
           <!-- Test if it exists in the local file? -->
           <xsl:when test="$locauthobjs[@name=$name] !=''">
              <xsl:value-of select="$locauthobjs[@name=$name]"/>
           </xsl:when>
           <xsl:otherwise>
              <!-- It seems to exist in the global file!-->
              <xsl:value-of select="$globauthobjs[@name=$name]"/>
           </xsl:otherwise>
        </xsl:choose>
     </xsl:if>
</xsl:template>

<xsl:template name="key_file">
  <xsl:param name="path"/>
  <xsl:variable name="totlen">
     <xsl:value-of select="string-length($path)"/>
  </xsl:variable>
  <xsl:if test="$totlen &gt; 0">
     <xsl:variable name="cert_pos">
        <xsl:value-of select="$totlen - string-length(substring-after($path,'cert//')) + 1"/>
     </xsl:variable>
     <xsl:variable name="cert_len">
        <xsl:value-of select="string-length(substring-before($path, ' ')) + string-length(substring-after($path,'cert//')) - $totlen"/>
     </xsl:variable>
     <xsl:value-of select="'key_file('"/>
     <xsl:value-of select="substring($path, $cert_pos, $cert_len)"/>
     <xsl:value-of select="','"/>
     <xsl:value-of select="substring($path, $totlen - string-length(substring-after($path,'priv//')) + 1)"/>
     <xsl:value-of select="')'"/>
  </xsl:if>
</xsl:template>

Here below is a snippet from the input file. There can also exists authentication-object:s in policyglobal.xml file, thats why <xsl:template name="authobjbyname"> is needed.

<config>
<authentication-objects>
 <authentication-object name="My_test_665_private_key">
   <type>plain-file</type>
   <path>cert//C:/Temp/Certs/cert1.bin priv//C:/Temp/Certs/priv-key1.prv</path>
 </authentication-object>
 <authentication-object name="MyTestProfileSecret">
   <type>pre-shared-key</type>
   <path>hoobar</path>
 </authentication-object>
 <authentication-object name="NewTestKey">
   <type>pre-shared-key</type>
   <path>funny_hey</path>
 </authentication-object>
</authentication-objects>
<policy-rules>
 <ipsec-rules>
   <rule name="CertificateAuthentication">
      <authentication>
        <object>
          My_test_665_private_key
        </object>
      </authentication>
   </rule>
   <!-- There exists several rules here! -->
 </ipsec-rules>
</policy-rules>
</config>

Desired output:
<!-- Those authentication-objects with type = 'plain-file' which are used in some rule should appear here. -->
<exteranal-key type="software" init-info="key_file(C:/Temp/Certs/cert1.bin,C:/Temp/Certs/priv-key1.prv)"/>

Hopefully you get the point.

Harry

Current Thread