xsl-list
[Top] [All Lists]

xsl:key containing mixed elements

2004-10-23 05:10:04
Hi

I have a problem that I'm trying to solve using <xsl:key> but I'm not
getting the results that I expected.

It's probably best to explain first what I want to do and then show how I
tried to use <xsl:key> to solve the problem.

I have a file containing a mixture of <UML:SimpleState> and
<UML:CompositeState> elements.  Each one has a name attribute, but in the
case of the <UML:CompositeState> element the name comprises of a state name
followed by a colon followed by a sub-machine state name.

What I want to do is simply read my file and report any duplicate state
names.

So, here's a simplified example of some of the data in my file:

<UML:SimpleState name="on" />
<UML:SimpleState name="off" />
<UML:SimpleState name="off" />
<UML:SimpleState name="standby" />
<UML:CompositeState name="rewind:rewinding" />
<UML:CompositeState name="forward:fastforwarding" />
<UML:CompositeState name="standby:waiting" />
<UML:CompositeState name="record:recording" />
<UML:CompositeState name="record:recording" />

I'd like to generate error messages for states 'off', 'standby' and 'record'
because they are duplicates.

This is how I tried to solve the problem.  It works for the 'off' case but
not for the 'standby' or the 'record' cases:

 <!-- Create an index containing the name of all simple states
      and the first part of the name of all composite states -->
 <xsl:key
  name="name"
  match="UML:CompositeState[(_at_)name]"
  use="substring-before(@name,':')" />
 <xsl:key
  name="name"
  match="UML:SimpleState[(_at_)name]"
  use="@name" />

  <xsl:template match='/'>
    <xsl:for-each select="//UML:CompositeState[(_at_)name]">
      <xsl:variable name='name' select="substring-before(@name,':')"/>
            <xsl:if test="count(key('name',$name))>1">
              <xsl:message>Duplicate state name: <xsl:value-of
select='$name'/></xsl:message>
            </xsl:if>
    </xsl:for-each>

    <xsl:for-each select="//UML:SimpleState[(_at_)name]">
      <xsl:variable name='name' select="@name"/>
            <xsl:if test="count(key('name',$name))>1">
              <xsl:message>Duplicate state name: <xsl:value-of
select='$name'/></xsl:message>
            </xsl:if>
    </xsl:for-each>
  </xsl:template>

Can anyone enlighten me please?

BTW I'm using xalan:
Vendor: Apache Software Foundation
Vendor URL: http://xml.apache.org/xalan-j

D:\s>java -version
java version "1.4.2_05"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_05-b04)
Java HotSpot(TM) Client VM (build 1.4.2_05-b04, mixed mode)


Regards
George

George James Software
Caché Tools, Training, Technology
www.georgejames.com
+44-1932-252568






<Prev in Thread] Current Thread [Next in Thread>