lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, 9 Jul 2007, Eva Schmidt wrote:
The problem with this are the duplicate labels, that should create different table instead of overwriting the old ones...

What I do for that sort of case is turn the old label into an array.

When I get a subtag, I do something like:

if Result[ subtag ] == nil then
  Result[ subtag ] = value  -- doesn't exist; make a simple subtag
else
  if Result[ subtag ][ 1 ] == nil then
    Temp = Result[ subtag ]  -- convert simple tag to an array
    Result[ subtag ] = {}
    Result[ subtag ][ 1 ] = Temp
    Result[ subtag ][ 2 ] = value
  else
    Result[ subtag ][ table.maxn( Result[ subtag ] ) + 1 ] =
      value -- append to an existing array
  end
end

That's off the cuff, so it probably doesn't work, but it's the basic
idea.

- The first tag of a given name creates a simple table to hold the
  contents of a tag.
- If the tag already exists
  - If it's not an array, convert it to one by moving the old contents
    to [ 1 ] and appending the new contents in [ 2 ]
  - Otherwise just append the new contents to the array

So given something like

<tag attribute="value1" />

I wind up with a table like:

Result = {
  [ 'tag' ] = {
    [ 'attribute' ] = 'value1'
  }
}

If I get something like:

<tag attribute="value1" />
<tag attribute="value2" />

I wind up with a table like:

Result = {
  [ 'tag' ] = {
    [ 1 ] = {
      [ 'attribute' ] = 'value1'
    }
    [ 2 ] = {
      [ 'attribute' ] = 'value2'
    }
  }
}

My parser treats tag content as if it were an attribute named _content,
so if I get something like:

<tag>value1</tag>

I get:

Result = {
  [ 'tag' ] = {
    [ '_content' ] = 'value1'
  }
}

which works for my application, but is not general-purpose.
--
roger ivie
rivie@ridgenet.net