XML schemas - list of allowed attributes / tags in position in XML

Is there a way to query XmlSchemaor XmlSchemaSetlist the available tags / attributes at a specific XML point? So say that my cursor is between <b>and </b>, and my circuit only allows an element <c/>, can I figure it out using something built into C #?

<tagset>
  <a></a>
  <b><!-- CURSOR IS HERE --></b>
</tagset>
+5
source share
2 answers

There is a way, but the Xml Schema specification is complicated, so it will take several efforts and several hundred lines of code.

GetExpectedParticles .NET XmlSchemaValidator . XmlSchemaSet, , XmlSchemaObject.

, node , , . node .

GetExpectedParticles . , , , , .

, , .

GetExpectedParticles, , :

      public static List<XmlSchemaObject> XsdExpectedElements(XmlSchemaSet schemaSet,
                    List<NodeDescriptor> nodePath)
      {
        List<XmlSchemaObject> elementNames = new List<XmlSchemaObject>();          

        NameTable nt = new NameTable();
        XmlNamespaceManager manager = new XmlNamespaceManager(nt);
        XmlSchemaValidator validator = new XmlSchemaValidator(nt, schemaSet, manager, XmlSchemaValidationFlags.None);

        // event handler sets validationErrorFound local field
        validator.ValidationEventHandler += new ValidationEventHandler(validator_ValidationEventHandler);
        validator.Initialize();

        XmlSchemaInfo xsInfo = new XmlSchemaInfo();
        int i = 0;
        foreach (nodeDescriptor nameUri in nodePath)
        {
            validator.ValidateElement(nameUri.LocalName, nameUri.NamespaceUri, xsInfo);

            if ((i >= siblingPosition && siblingPosition > -1) || nameUri.Closed)
            {
                validator.SkipToEndElement(null);
            }
            else
            {
                validator.ValidateEndOfAttributes(null);
            }
            i++;
        }

        XmlSchemaParticle[] parts = validator.GetExpectedParticles();
        if (parts.Length == 0)
        {
            bool hasElements = true;
            bool elementClosed = nodePath[nodePath.Count - 1].Closed;
            if (elementClosed) // we're outside the element tags
            {
                hasElements = true;
            }
            else if (xsInfo.SchemaType is XmlSchemaSimpleType)
            {
                hasElements = false;
            }
            else
            {
                XmlSchemaComplexType xsCt = xsInfo.SchemaType as XmlSchemaComplexType;
                XmlSchemaContentType xsContent = (XmlSchemaContentType)xsCt.ContentType;
                if (xsContent == XmlSchemaContentType.TextOnly)
                {
                    hasElements = false;
                }
            }
            if (!hasElements)
            {
                expectedType = XmlEditor.expectedListType.elementValue;
                if (xsInfo.SchemaElement != null)
                {
                    elementNames.Add(xsInfo.SchemaElement);
                }
            }
            return elementNames;
        }

        foreach (XmlSchemaObject xso in parts)
        {
            if (xso is XmlSchemaElement)
            {
                XmlSchemaElement xse = (XmlSchemaElement)xso;
                if (subGroupList.ContainsKey(xse.QualifiedName))
                {
                    List<XmlSchemaElement> xses = subGroupList[xse.QualifiedName];
                    foreach (XmlSchemaElement xseInstance in xses)
                    {
                        elementNames.Add(xseInstance);
                    }
                }
                else
                {
                    elementNames.Add(xse);
                }
            }
            else if (xso is XmlSchemaAny)
            {
                XmlSchemaAny xsa = (XmlSchemaAny)xso;
                foreach (XmlSchema xs in schemaSet.Schemas())
                {
                    if (xs.TargetNamespace == xsa.Namespace)
                    {
                        foreach (XmlSchemaElement xseAny in xs.Elements)
                        {
                            elementNames.Add(xseAny);
                        }
                    }
                }
            }
        }
    }

() , :

    private List<string> ExpectedEnumValues(XmlSchemaObject xsso)
    {
        XmlSchemaSimpleType xst = null;
        XmlSchemaComplexType xsCt = null;
        List<string> values = new List<string>();
        if (xsso == null)
        {
            return values;
        }
        if (xsso is XmlSchemaAttribute)
        {
            XmlSchemaAttribute xsa = (XmlSchemaAttribute)xsso;
            xst = xsa.AttributeSchemaType;
        }
        else
        {
            XmlSchemaElement xse = (XmlSchemaElement)xsso;
            XmlSchemaType gxst = xse.ElementSchemaType;
            if (gxst is XmlSchemaSimpleType)
            {
                xst = (XmlSchemaSimpleType)gxst;
            }
            else if (gxst is XmlSchemaComplexType)
            {
                xsCt = (XmlSchemaComplexType)gxst;
            }
            else
            {
                return values;
            }
        }

        if(xst != null)
        {
            if (xst.TypeCode == XmlTypeCode.Boolean)
            {
                values.Add("true");
                values.Add("false");
            }
            else
            {
                ProcessXmlSimpleType(xst, values);
            }
        }
        else if (xsCt != null)
        {
            XmlSchemaContentType xsContent = (XmlSchemaContentType) xsCt.ContentType;
            XmlSchemaContentModel xsModel = (XmlSchemaContentModel)xsCt.ContentModel;
            if (xsModel is XmlSchemaSimpleContent)
            {
                XmlSchemaSimpleContent xsSC = (XmlSchemaSimpleContent)xsModel;
                XmlSchemaContent xsRE = xsSC.Content;
                if (xsRE != null)
                {
                    if (xsRE is XmlSchemaSimpleContentRestriction)
                    {
                        XmlSchemaSimpleContentRestriction xsCCR = (XmlSchemaSimpleContentRestriction)xsRE;
                        foreach (XmlSchemaObject xso in xsCCR.Facets)
                        {
                            if (xso is XmlSchemaEnumerationFacet)
                            {
                                XmlSchemaEnumerationFacet xsef = (XmlSchemaEnumerationFacet)xso;
                                values.Add(xsef.Value);
                            }
                        }
                    }
                }
            }
            else
            {
                XmlSchemaComplexContent xsCC = (XmlSchemaComplexContent)xsModel;
                XmlSchemaContent xsRE = xsCC.Content;
                if (xsRE != null)
                {
                    if (xsRE is XmlSchemaComplexContentRestriction)
                    {
                        XmlSchemaComplexContentRestriction xsR = (XmlSchemaComplexContentRestriction)xsRE;

                    }
                    else if (xsRE is XmlSchemaComplexContentExtension)
                    {
                        XmlSchemaComplexContentExtension xsE = (XmlSchemaComplexContentExtension)xsRE;
                    }
                }
            }


        }

        return values;
    }

:

    private static void ProcessXmlSimpleType(XmlSchemaSimpleType xst, List<string> values)
    {
        if (xst == null)
        {
            return;
        }
        XmlSchemaSimpleTypeContent xsstc = xst.Content;
        if (xsstc is XmlSchemaSimpleTypeRestriction)
        {
            XmlSchemaSimpleTypeRestriction xsr = (XmlSchemaSimpleTypeRestriction)xsstc;
            XmlSchemaObjectCollection xsoc = xsr.Facets;

            XmlSchemaSimpleType bastTypeOfRestiction = xsr.BaseType;
            foreach (XmlSchemaObject xso in xsoc)
            {
                if (xso is XmlSchemaEnumerationFacet)
                {
                    XmlSchemaEnumerationFacet xsef = (XmlSchemaEnumerationFacet)xso;
                    values.Add(xsef.Value);
                }
            }
        }
        else if (xsstc is XmlSchemaSimpleTypeList)
        {
            XmlSchemaSimpleTypeList xsstL = (XmlSchemaSimpleTypeList)xsstc;
            XmlSchemaSimpleType xstL = xsstL.BaseItemType;
            ProcessXmlSimpleType(xstL, values); // recursive
        }
        else if (xsstc is XmlSchemaSimpleTypeUnion)
        {
            XmlSchemaSimpleTypeUnion xstU = (XmlSchemaSimpleTypeUnion)xsstc;
            XmlSchemaSimpleType[] xsstArray = xstU.BaseMemberTypes;
            foreach (XmlSchemaSimpleType xsstA in xsstArray)
            {
                ProcessXmlSimpleType(xsstA, values); // recursive
            }
        }
    }

, , 20% , , , , , ..NET , XML Schema.

XML - , XML , , , , - " ", ".

XML XML .NET , . , .NET XML .

+5

LGPL SharpDevelops XmlEditor. xml dll, XmlEditor.dll AddIns/DisplayBindings.

0

All Articles