Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>PROBLEM 1.</p> <p>For the first problem that you have you can use a key:</p> <pre><code>&lt;xsl:key name="variable-key" match="//variable" use="@name" /&gt; </code></pre> <p>That key is going to index all variable elements in the document, using their name. So now, we can access any of those elements by using the following XPath expression:</p> <pre><code>key('variable-key', 'X') </code></pre> <p>Using this approach is efficient when you have a lot of variable elements.</p> <p><em>NOTE: this approach is not valid if each variable has its own scope (i.e. you have local variables which are not visible in different parts of the document). In that case this approach should be modified.</em></p> <p>PROBLEM 2.</p> <p>For mapping attributes you could use a template like the following:</p> <pre><code> &lt;xsl:template match="@baseType[. = 'int']"&gt; &lt;xsl:attribute name="baseType"&gt; &lt;xsl:value-of select="'Long'" /&gt; &lt;/xsl:attribute&gt; &lt;/xsl:template&gt; </code></pre> <p>The meaning of this transformation is: <em>each time that we match a baseType attribute with int value, it has to be replaced by a Long value.</em></p> <p>This transformation would be in place for each @baseType attribute in the document.</p> <hr> <p>Using the described strategies a solution could be:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="xml" indent="yes" /&gt; &lt;!-- Index all variable elements in the document by name --&gt; &lt;xsl:key name="variable-key" match="//variable" use="@name" /&gt; &lt;!-- Just for demo --&gt; &lt;xsl:template match="text()" /&gt; &lt;!-- Identity template: copy attributes by default --&gt; &lt;xsl:template match="@*"&gt; &lt;xsl:copy&gt; &lt;xsl:value-of select="." /&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;!-- Match the structure type --&gt; &lt;xsl:template match="type[@baseType='structure']"&gt; &lt;Item&gt; &lt;xsl:apply-templates select="*|@*" /&gt; &lt;/Item&gt; &lt;/xsl:template&gt; &lt;!-- Match the variable instance --&gt; &lt;xsl:template match="variableInstance"&gt; &lt;Field&gt; &lt;!-- Use the key to find the variable with the current name --&gt; &lt;xsl:apply-templates select="@*|key('variable-key', @name)/@baseType" /&gt; &lt;/Field&gt; &lt;/xsl:template&gt; &lt;!-- Ignore attributes with baseType = 'structure' --&gt; &lt;xsl:template match="@baseType[. = 'structure']" /&gt; &lt;!-- Change all baseType attributes with long values to an attribute with the same name but with an int value --&gt; &lt;xsl:template match="@baseType[. = 'int']"&gt; &lt;xsl:attribute name="baseType"&gt; &lt;xsl:value-of select="'Long'" /&gt; &lt;/xsl:attribute&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>That code is going to transform the following XML document:</p> <pre><code>&lt;!-- The code element is present just for demo --&gt; &lt;code&gt; &lt;variable baseType="int" name="X" /&gt; &lt;type baseType="structure" name="Y"&gt; &lt;variableInstance name="X" /&gt; &lt;/type&gt; &lt;/code&gt; </code></pre> <p>into</p> <pre><code>&lt;Item name="Y"&gt; &lt;Field baseType="Long" name="X"/&gt; &lt;/Item&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. This table or related slice is empty.
    1. 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