Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I understand your requirement correctly then <code>@country</code> always exists so we can do a key based lookup on that attribute. For the other two attributes <code>@areacode</code> and <code>@division</code> you want take the <code>code</code> which has both attributes matching with priority over a <code>code</code> which has one matching attribute with priority over a <code>code</code> with no matching attribute.</p> <p>So I would simply create a sequence of the different priorities and take first one found:</p> <pre><code>&lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:param name="lkp-url" select="'test2013062402.xml'"/&gt; &lt;xsl:variable name="lkp-doc" select="doc($lkp-url)"/&gt; &lt;xsl:key name="by-country" match="code" use="@country"/&gt; &lt;xsl:param name="c" select="'ca'"/&gt; &lt;xsl:param name="ac" select="'222'"/&gt; &lt;xsl:param name="d" select="'46'"/&gt; &lt;xsl:template match="/"&gt; &lt;xsl:value-of select="(key('by-country', $c, $lkp-doc)[@areacode = $ac and @division = $d], key('by-country', $c, $lkp-doc)[not(@areacode) and @division = $d], key('by-country', $c, $lkp-doc)[@areacode = $ac and not(@division)], key('by-country', $c, $lkp-doc)[not(@areacode) and not(@division)])[1]/@id"/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>I don't know which of the two attributes has priority, you might want to shuffle the second and third item in the sequence I created in the code if <code>areacode</code> has priority.</p> <p>The above code should be shortened to</p> <pre><code>&lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:param name="lkp-url" select="'test2013062402.xml'"/&gt; &lt;xsl:variable name="lkp-doc" select="doc($lkp-url)"/&gt; &lt;xsl:key name="by-country" match="code" use="@country"/&gt; &lt;xsl:param name="c" select="'ca'"/&gt; &lt;xsl:param name="ac" select="'222'"/&gt; &lt;xsl:param name="d" select="'46'"/&gt; &lt;xsl:template match="/"&gt; &lt;xsl:variable name="cs" select="key('by-country', $c, $lkp-doc)"/&gt; &lt;xsl:value-of select="($cs[@areacode = $ac and @division = $d], $cs[not(@areacode) and @division = $d], $cs[@areacode = $ac and not(@division)], $cs[not(@areacode) and not(@division)])[1]/@id"/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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