{"record":{"id":"e922f828be86e3ed","repo":"iOfficeAI/OfficeCLI","slug":"invalid-path-index-indexstr-in-segment-part","errorCode":null,"errorMessage":"Invalid path index '{indexStr}' in segment '{part}'. Expected a numeric index.","messagePattern":"Invalid path index '(.+?)' in segment '(.+?)'\\. Expected a numeric index\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Core/GenericXmlQuery.cs","lineNumber":261,"sourceCode":"            var bracketIdx = part.IndexOf('[');\n            if (bracketIdx >= 0)\n            {\n                // BUG-R36-01 fix: when ']' is missing (e.g. \"slide[\") the expression\n                // part[(bracketIdx+1)..^1] produces a negative-length range crash.\n                // Detect and reject unclosed brackets with a clean ArgumentException.\n                var closingIdx = part.IndexOf(']', bracketIdx + 1);\n                if (closingIdx < 0)\n                    throw new ArgumentException($\"Malformed path segment '{part}'. Bracket '[' is not closed. Expected format: name[index] or name[@attr=value].\");\n                var name = PathAliases.Resolve(part[..bracketIdx]);\n                var indexStr = part[(bracketIdx + 1)..^1];\n                if (!int.TryParse(indexStr, out var idx))\n                    // A predicate in the index slot (row[Score>0], row[not(V)])\n                    // means the caller reached the single-node path navigator\n                    // with a FILTER. get is one-node-by-path by contract;\n                    // point at the verbs that run the selector engine.\n                    throw new ArgumentException(AttributeFilter.IsContentFilterPath($\"[{indexStr}]\")\n                        ? $\"'{part}' is a predicate, but this verb navigates by position and expects a numeric index (e.g. {part[..part.IndexOf('[')]}[2]). Predicates work on 'query' (read) and 'set'/'remove' (mutate matched elements).\"\n                        : $\"Invalid path index '{indexStr}' in segment '{part}'. Expected a numeric index.\");\n                if (idx < 1)\n                    throw new ArgumentException($\"Invalid path index '{idx}' in segment '{part}'. Index must be >= 1.\");\n                segments.Add((name, idx));\n            }\n            else\n            {\n                segments.Add((PathAliases.Resolve(part), null));\n            }\n        }\n        return segments;\n    }\n\n    /// <summary>\n    /// Navigate an OpenXML element tree by path segments (localName + optional 1-based index).\n    /// Returns null if any segment cannot be resolved.\n    /// </summary>\n    public static OpenXmlElement? NavigateByPath(OpenXmlElement root, IReadOnlyList<(string Name, int? Index)> segments)\n    {","sourceCodeStart":243,"sourceCodeEnd":279,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Core/GenericXmlQuery.cs#L243-L279","documentation":"Thrown by the GenericXmlQuery path parser when the bracket content is neither a numeric index nor a recognized predicate. After extracting the text between '[' and ']', int.TryParse fails and IsContentFilterPath returns false, so the parser cannot interpret the segment as a position or a filter.","triggerScenarios":"Calling a path navigator with a segment like 'row[abc]' or 'slide[1.5]' — non-numeric, non-predicate bracket content that fails both int.TryParse and the predicate check.","commonSituations":"Typo in an index; attempting zero-based indexing (the parser expects 1-based); a malformed predicate not recognized by the filter detector; leftover placeholder text.","solutions":["Use a 1-based numeric index (e.g. row[2], not row[1.5] or row[abc]).","If you meant a predicate, use recognized predicate syntax (e.g. @attr=value or a content filter) on the query/set/remove verbs.","Check for typos or placeholder text inside the brackets."],"exampleFix":"// before\nGet(xml, \"rows/row[two]\"); // not numeric, not a predicate\n// after\nGet(xml, \"rows/row[2]\");","handlingStrategy":"validation","validationCode":"static bool IsIndexSegment(string segment)\n{\n    var b = segment.IndexOf('[');\n    if (b < 0) return true;\n    var inner = segment[(b + 1)..^1];\n    return int.TryParse(inner, out _);\n}","typeGuard":null,"tryCatchPattern":"try { Get(xml, path); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Expected a numeric index\"))\n{ /* convert to numeric index or switch verb */ }","preventionTips":["Use 1-based numeric indices for positional navigation.","Route predicate-style filters to the query verb."],"tags":["xml","path","validation","xpath-like"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}