Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I use a variable to resolve sub elements where the variable reflects a node more than one element deep?
    primarykey
    data
    text
    <p>Here's what I've got.</p> <p>My data: <strong>data.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;?xml-stylesheet type="text/xsl" href="myxslt2.xslt"?&gt; &lt;data&gt; &lt;foo&gt; &lt;innerfoo1&gt;inner-foo-1-text&lt;/innerfoo1&gt; &lt;innerfoo2&gt;inner-foo-2-text&lt;/innerfoo2&gt; &lt;/foo&gt; &lt;bar&gt;Hello World&lt;/bar&gt; &lt;foobar&gt;This is a test&lt;/foobar&gt; &lt;/data&gt; </code></pre> <p>My metadata - this is tell the xslt which of the data nodes are to be displayed. </p> <p><strong>metadata.xml</strong></p> <pre><code>&lt;Metadata&gt; &lt;Data&gt; &lt;Detail&gt;foobar&lt;/Detail&gt; &lt;Detail&gt;bar&lt;/Detail&gt; &lt;Detail&gt;foo/innerfoo1&lt;/Detail&gt; &lt;/Data&gt; &lt;/Metadata&gt; </code></pre> <p>We want to display everything except innerfoo2. </p> <p>My xslt: <strong>myxslt.xsl</strong></p> <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" version="1.0"&gt; &lt;xsl:output method="html" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" indent="yes"/&gt; &lt;xsl:variable name="main" select="/data"/&gt; &lt;xsl:template name="myTemplate"&gt; &lt;xsl:param name="myparam"/&gt; &lt;xsl:param name="node"/&gt; Node: &lt;xsl:value-of select="$node"/&gt;&lt;br/&gt; Inner:&lt;xsl:value-of select="msxsl:node-set($myparam)/data/*[local-name() = $node][1]"/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="/data"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt; HTML STARTS &lt;br/&gt; &lt;xsl:variable name="data" select="."/&gt; Outer1:&lt;xsl:value-of select="$data"/&gt; &lt;br/&gt; Outer2:&lt;xsl:value-of select="$data/foobar"/&gt; &lt;br/&gt; &lt;xsl:variable name="defaultMetadata" select="document('metadata.xml')"/&gt; &lt;xsl:for-each select="msxsl:node-set($defaultMetadata)/Metadata/Data/Detail"&gt; &lt;br/&gt;----&lt;br/&gt; &lt;xsl:call-template name="myTemplate"&gt; &lt;xsl:with-param name="node"&gt; &lt;xsl:value-of select="."&gt;&lt;/xsl:value-of&gt; &lt;/xsl:with-param&gt; &lt;xsl:with-param name="myparam"&gt; &lt;xsl:copy-of select="$data"/&gt; &lt;/xsl:with-param&gt; &lt;/xsl:call-template&gt; &lt;/xsl:for-each&gt; &lt;/html&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>(pastebin for better readability - <a href="http://pastebin.com/Uw7bFYWM" rel="nofollow">http://pastebin.com/Uw7bFYWM</a> ) </p> <p><strong>Output:</strong></p> <pre><code>HTML STARTS Outer1: inner-foo-1-text inner-foo-2-text Hello World This is a test Outer2:This is a test ---- Node: foobar Inner:This is a test ---- Node: bar Inner:Hello World ---- Node: foo/innerfoo1 Inner: </code></pre> <p>So what I'm doing is looping through each detail element of the metadata, and calling the template passing in the data, the name of of the node to be displayed. </p> <p>The template then resolves that node and displays it. </p> <p>So you can see here that it resolves single level elements fine, but I can't use that <code>local-name() = $node</code> comparison when it's more than one element deep. </p> <p>What I'd like to do is something like:</p> <pre><code>Inner:&lt;xsl:value-of select="msxsl:node-set($myparam)/data/$node"/&gt; </code></pre> <p>But this doesn't work. </p> <p>How can achieve this? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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