Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic variable access within XSLT
    text
    copied!<p>I would like to define an xslt sheet where variables and access functions are defined. The sheet should be then used within another stylesheet. The main idea is to have single point of defining key value pairs for interface mapping tasks. The following code does not produce a result.</p> <p><strong>EDIT:</strong> The real world problem is a system integration problem. Think of a "type"-property. In System A the type "Car" is encoded with the key "1". In System B (where i have to import a message from System A) the type "Car" is encoded with the key "X". Example: 1 &lt;-> Car &lt;-> X. So when i map messages from System A to messages from System B i need kind of a map. I would like to define this map at a single place (single XSLT sheet) and use it in multiple other XSLT sheet (via incluce command). As long with the map i also would like to define convenient access functions like this: getTargetKey(mapWhereTheKeyIsIn, 'sourceKey'). So where ever i have to map message from System A into the schema of System B i would like to type: <code>&lt;Type&gt;&lt;xsl:value-of select="getTargetKey(typeMap, '1')" /&gt;&lt;Type&gt;</code> and get <code>&lt;Type&gt;X&lt;/Type&gt;</code></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"&gt; &lt;xsl:output method="text" encoding="UTF-8"/&gt; &lt;xsl:variable name="var1"&gt; &lt;Entry key="1" value="ABC" /&gt; &lt;Entry key="2" value="CDF" /&gt; &lt;/xsl:variable&gt; &lt;xsl:template match="/"&gt; &lt;xsl:value-of select="myFunc('var1', '1')" /&gt; &lt;/xsl:template&gt; &lt;xsl:function name="myFunc"&gt; &lt;xsl:param name="variable_name"/&gt; &lt;xsl:param name="key"/&gt; &lt;xsl:value-of select="document('')/*/xsl:variable[@name=$variable_name]/Entry[@key = $key]/@value"/&gt; &lt;/xsl:function&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>Sample Input</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;order_system_a type="1" /&gt; </code></pre> <p><strong>Desired Output</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;order_system_b type="X" /&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload