This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Replacing a string by another which is found by reference in the same XML document


Hi Vincent,

>My source XML contains something like:
> <Root>
> <Class
>      Name="MyClass"
>      Uuid="80A2B3BD-0000-520C-383BE4980006BE67"
>      TargetRef="80A2B3BD-0000-520C-383BE4980006A75A">
></Class>
>
><AnotherObject
>Name="MyObject"
>Uuid="80A2B3BD-0000-520C-383BE4980006B687">
></AnotherObject>
></Root>
>
>I have an XSL style sheet  to display it on IE5 with apropriate style. I
>would like to get as output:
>
>Class:
>Name="MyClass"
>TargetRef="MyObject"
>
>Where the TargetRef string has been replaced by the value of the string
>with the same Uuid.

This seems to me to be a good instance to use xsl:key to identify the nodes that are uniquely identified through the 'Uuid' attribute.  First, set up the key:
* name  - a name for the key, anything you like
* match - an XPath matching the nodes that you want to identify
* use   - an XPath (relative to the 'match' node) that identifies the node

In your case:

<xsl:key name="objects" match="*[@Uuid]" use="@Uuid" />

Note that I haven't named the (element) nodes that are identified by the key because it isn't clear to me whether your 'Class' and 'AnotherObject' elements are indicative of a whole range of possible element names in your input, but we can guarantee at least that they will have a 'Uuid' attribute if they're worth identifying!

Then you can access a particular node through its 'Uuid' attribute using the key() function, so try:

<xsl:template match ="Class">
  Class:
  Name="<xsl:value-of select="@Name" />"
  TargetRef="<xsl:value-of select="key('objects', @TargetRef)/@Name" />"
</xsl:template>

This works in SAXON.  I'm not sure about you using IE5: it depends on what version of MSXML you have - the old one at least didn't support keys.

I hope that helps,

Jeni

Jeni Tennison
http://friday.u-net.com/jeni/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]