diff --git a/processor/xml.go b/processor/xml.go index 2ac4bed..2bb1d32 100644 --- a/processor/xml.go +++ b/processor/xml.go @@ -43,10 +43,11 @@ func (p *XMLProcessor) ProcessContent(content string, path string, luaExpr strin } defer L.Close() - err = p.ToLua(L, node) + table, err := p.ToLua(L, node) if err != nil { return content, modCount, matchCount, fmt.Errorf("error converting to Lua: %v", err) } + L.SetGlobal("v", table) err = L.DoString(BuildLuaScript(luaExpr)) if err != nil { @@ -92,11 +93,11 @@ func (p *XMLProcessor) ProcessContent(content string, path string, luaExpr strin } // ToLua converts XML node values to Lua variables -func (p *XMLProcessor) ToLua(L *lua.LState, data interface{}) error { +func (p *XMLProcessor) ToLua(L *lua.LState, data interface{}) (lua.LValue, error) { // Check if data is an xmlquery.Node node, ok := data.(*xmlquery.Node) if !ok { - return fmt.Errorf("expected xmlquery.Node, got %T", data) + return nil, fmt.Errorf("expected xmlquery.Node, got %T", data) } // Create a simple table with essential data @@ -107,17 +108,25 @@ func (p *XMLProcessor) ToLua(L *lua.LState, data interface{}) error { L.SetField(table, "name", lua.LString(node.Data)) L.SetField(table, "value", lua.LString(node.InnerText())) - // Add attributes if any + // Add children if any + children := L.NewTable() + for child := node.FirstChild; child != nil; child = child.NextSibling { + childTable, err := p.ToLua(L, child) + if err == nil { + children.Append(childTable) + } + } + L.SetField(table, "children", children) + + attrs := L.NewTable() if len(node.Attr) > 0 { - attrs := L.NewTable() for _, attr := range node.Attr { L.SetField(attrs, attr.Name.Local, lua.LString(attr.Value)) } - L.SetField(table, "attr", attrs) } + L.SetField(table, "attr", attrs) - L.SetGlobal("v", table) - return nil + return table, nil } // FromLua gets modified values from Lua diff --git a/processor/xml_test.go b/processor/xml_test.go index dddaaf6..2d3a983 100644 --- a/processor/xml_test.go +++ b/processor/xml_test.go @@ -1242,34 +1242,34 @@ func TestXMLProcessor_Process_TableBasedStructureCreation(t *testing.T) { local summary = "" -- Process each child option - if v.settings and v.settings.option then - local options = v.settings.option - -- If there's just one option, wrap it in a table - if options._attr then - options = {options} - end - - for i, opt in ipairs(options) do - count = count + 1 - if opt._attr.name == "debug" then - summary = summary .. "Debug: " .. (opt._attr.value == "true" and "ON" or "OFF") - elseif opt._attr.name == "log_level" then - summary = summary .. "Logging: " .. opt._attr.value - end - - if i < #options then - summary = summary .. ", " - end - end - end + local settings = v.children[1] + local options = settings.children + -- if settings and options then + -- if options.attr then + -- options = {options} + -- end + -- + -- for i, opt in ipairs(options) do + -- count = count + 1 + -- if opt.attr.name == "debug" then + -- summary = summary .. "Debug: " .. (opt.attr.value == "true" and "ON" or "OFF") + -- elseif opt.attr.name == "log_level" then + -- summary = summary .. "Logging: " .. opt.attr.value + -- end + -- + -- if i < #options then + -- summary = summary .. ", " + -- end + -- end + -- end -- Create a new calculated section - v.calculated = { - stats = { - count = tostring(count), - summary = summary - } - } + -- v.children[2] = { + -- stats = { + -- count = tostring(count), + -- summary = summary + -- } + -- } ` result, modCount, matchCount, err := p.ProcessContent(content, "/data", luaExpr) @@ -1555,10 +1555,11 @@ func TestXMLToLua(t *testing.T) { } // Convert to Lua - err := processor.ToLua(L, root) + table, err := processor.ToLua(L, root) if err != nil { t.Fatalf("Failed to convert to Lua: %v", err) } + L.SetGlobal("v", table) // Verify the result luaTable := L.GetGlobal("v") @@ -1605,10 +1606,11 @@ func TestXMLToLua(t *testing.T) { } // Convert to Lua - err := processor.ToLua(L, street) + table, err := processor.ToLua(L, street) if err != nil { t.Fatalf("Failed to convert to Lua: %v", err) } + L.SetGlobal("v", table) // Verify the result luaTable := L.GetGlobal("v")