In XSLT 1.0 how to create a lookup function on XML data embedded in the XSLT using key and document('')? -


my specific need lookup line-heights given particular font-size, seeking learn general technique of creating lookups/a specific mapping.

i think possible embed xml in xslt document (so can work standalone) , build key on using document('') function reference current xslt, this:

<xsl:variable name="data.font-metrics.line-height">     <line-height font-size="11">12</line-height>     <line-height font-size="12">14</line-height>     <line-height font-size="13">15</line-height>     <line-height font-size="14">16</line-height>     <line-height font-size="15">17</line-height>     <line-height font-size="16">18</line-height>     <line-height font-size="17">20</line-height>     <line-height font-size="18">21</line-height> </xsl:variable> <xsl:key name="lookup.font-metrics.line-height" match="document('')//xsl:variable[@name='data.font-metrics.line-height'])/line-height" use="@font-size"/> 

after that, should able lookup line height using key function:

<xsl:value-of select="key('lookup.font-metrics.line-height',$font-size)"/> 

...however getting following error message:

xpath error : invalid expression //document('')//xsl:variable[@name='data.font-metrics.line-height'])/line-height/text()            ^ 

i think several problems coming here:

  • use of document function
  • use of key function
  • what best method of embedding xml? in variable?

there may different solution problem.

i grateful of help!

in xslt 1.0, key() function works in context of current document (in xslt 2.0 has third argument, allowing select context). in order use key on nodes in document, must first switch context document - example:

xslt 1.0

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>  <xsl:variable name="data.font-metrics.line-height">     <line-height font-size="11">12</line-height>     <line-height font-size="12">14</line-height>     <line-height font-size="13">15</line-height>     <line-height font-size="14">16</line-height>     <line-height font-size="15">17</line-height>     <line-height font-size="16">18</line-height>     <line-height font-size="17">20</line-height>     <line-height font-size="18">21</line-height> </xsl:variable>  <xsl:key name="lookup.font-metrics.line-height" match="line-height" use="@font-size"/>  <xsl:template match="/">     <xsl:param name="font-size" select="14"/>     <output>         <!-- other nodes -->         <!-- switch context stylesheet in order use key -->         <xsl:for-each select="document('')">             <lookup>                 <xsl:value-of select="key('lookup.font-metrics.line-height', $font-size)"/>             </lookup>         </xsl:for-each>         <!-- more nodes -->     </output> </xsl:template>  </xsl:stylesheet> 

result

<?xml version="1.0" encoding="utf-8"?> <output>    <lookup>16</lookup> </output> 

note xsl:key element not playing part in switch , can defined in simpler terms.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -