The following sequence cannot work:
---
<command name="getTest" parameter="%_" />
<get expression="$view:preview"/>
<set expression="$saveSelected" variable="selected"></set>
<command name="putAttribute"
parameter="[implicitElement] vendor '%_'" />
---
because by invoking <set> after the <get>, you clear variable %_.
I don't want to show off but what you need to do can be done much simply
by using the full power of XPath.
I have tested this with DocBook and it works fine:
---
<binding>
<keyPressed code="F5" />
<command name="test" />
</binding>
<command name="test">
<macro trace="true">
<sequence>
<command name="insert" parameter="into link"/>
<command name="putAttribute" parameter="linkend"/>
<get context="$selected" expression="id(@linkend)" />
<command name="putAttribute" parameter="vendor '%_'"/>
</sequence>
</macro>
</command>
---
Note that you *may* need to write (depends on your DTD):
---
<get context="$selected" expression="string(id(@linkend)/*[1])" />
---
instead of simply:
---
<get context="$selected" expression="id(@linkend)" />
---
Kevin Flynn wrote:
> Everything is happening in one view of one document. However, I have done
> some further testing and found that:
>
> 1 The code I originally sent contains an error: the line
> <cfg:set expression="id(%0)" variable="selected"></cfg:set>
> should. of course be
> <cfg:set expression="id('%0')" variable="selected"></cfg:set>
> since %0 is an XXE variable, not an XPath variable
>
> 2 Fixing this error didn't help.
>
> The code example I sent was based on my own DTD and therefore difficult for
> you to debug without a document, DTD, etc. I have therefore created a couple
> of macro that reproduce the problem in DocBook:
>
> <command name="test">
> <macro trace="true">
> <sequence>
> <command name="insert" parameter="into link" />
> <command name="putAttribute" parameter="[implicitElement] linkend"
> />
> <command name="delete" parameter="[implicitElement]" />
> <command name="undo" />
>
> <set variable="id" context="$selected" expression="./@linkend" />
>
> <!-- comment out next line for version 2 -->
> <set expression="$selected" variable="saveSelected"></set>
>
> <!-- uncomment next line for version 2 -->
> <!-- <set expression="$selected"
> variable="view:saveSelected"></set>-->
>
> <get expression="$id"/>
> <command name="getTest" parameter="%_" />
> <get expression="$view:preview"/>
>
> <!-- comment out next line for version 2 -->
> <set expression="$saveSelected" variable="selected"></set>
> <command name="putAttribute" parameter="[implicitElement] vendor
> '%_'" />
> </sequence>
> </macro>
> </command>
>
> <command name="getTest">
> <macro>
> <sequence>
> <set expression="id('%0')" variable="selected"></set>
> <command name="selectNode" parameter="firstChild"/>
> <get context="$selectedNode" expression="."/>
> <set context="$selectedNode" expression="."
> variable="view:preview"></set>
> <!-- uncomment next line for version 2 -->
> <!-- <set expression="$view:saveSelected" variable="selected"/>-->
> </sequence>
> </macro>
> </command>
>
> The macro 'test' should:
>
> 1 Insert a link element.
> 2 Prompt you for the 'linkend' attribute.
> 3 Copy the content of the first child of the 'linkend' element into the
> link element's 'vendor' attribute.
>
> Undoubtedly a very silly sequence of actions, but it illustrates the point
> (i.e, doesn't work - an empty string is inserted into the 'vendor'
> attribute).
>
> However, if you then modify the macros to "version 2" by
> commenting/uncommenting as indicated and run "test" again, it works. The
> difference seems to be that if the relocation of the node selection is done
> in the _called_ macro, then everything is OK. If the relocation is done by
> the _calling_ macro, however, then it fails.
>
> That's my theory so far, at least (and the big plus is I seem to have found
> my own workaround!)
>
> Kevin Flynn
>
>
> -----Original Message-----
> From: Hussein Shafie [mailto:hussein at pixware.fr]
> Sent: 11. juni 2004 10:03
> To: Kevin Flynn
> Cc: xmleditor-support at xmlmind.com
> Subject: Re: [XXE] User variable scope
>
>
> Kevin Flynn wrote:
>
>>I am having trouble sharing user variables between macros.
>>
>>The following code works:
>>...
>> <cfg:set expression="$selected"
>>variable="saveSelected"></cfg:set>
>> <cfg:set expression="id($id)" variable="selected"></cfg:set>
>> <cfg:command name="selectNode" parameter="firstChild"/>
>> <cfg:set context="$selectedNode" expression="."
>>variable="preview"></cfg:set>
>> <cfg:set expression="$saveSelected"
>>variable="selected"></cfg:set>
>> <cfg:get expression="$preview"/>
>> <cfg:command name="putAttribute" parameter="[implicitElement]
>
> preview
>
>>'%_'" />
>>...
>>
>>If, however, I try to move a portion of the code to a different macro and
>>call it, as follows:
>>
>>...
>> <cfg:set expression="$selected"
>>variable="saveSelected"></cfg:set>
>> <cfg:get expression="$id"/>
>> <cfg:command name="getPreview" parameter="%_" />
>> <cfg:get expression="$view:preview"/>
>> <cfg:command name="putAttribute" parameter="[implicitElement]
>
> preview
>
>>'%_'" />
>>...
>>
>> <cfg:command name="getPreview">
>> <cfg:macro>
>> <cfg:sequence>
>> <cfg:set expression="id(%0)" variable="selected"></cfg:set>
>> <cfg:command name="selectNode" parameter="firstChild"/>
>> <cfg:set context="$selectedNode" expression="."
>>variable="view:preview"></cfg:set>
>> </cfg:sequence>
>> </cfg:macro>
>> </cfg:command>
>>
>>it no longer works: the <cfg:get expression="$view:preview"/> returns an
>>empty string.
>>
>>I have included the following namespace declaration in the root
>>configuration element:
>>
>>
>> xmlns:view="http://www.xmlmind.com/xmleditor/namespace/scope/view"
>>
>>What am I doing wrong?
>
>
> At first glance, I don't see anything wrong. Please note that variable
> "view:preview" has a *document view scope*, not a document scope, not a
> global scope.
>
> This means that what you did should work within the same ``document
> window'' but not in the ``window'' of another document and not in
> another ``window'' (with a different style sheet) of the same document.
>
> If this is not the case, may be you have found a bug and in such case,
> send us an new email reporting this.