-5

Can someone please help me with the below XML?

<?xml version="1.0" encoding="UTF-8" ?>
<xs:collection-list xmlns:xs="http://www.hp.com/hpsim7.5.0.0" 
                    elementFormDefault="qualified">    
  <xs:collection name="abc" 
                 type="system" 
                 parent="Systems by Type">
    <member name="All Servers" 
            type="query" 
            display-status="0"default-view="tableview" 
            hidden="false" />
  </collection>
</collection-list>

I am not sure what's wrong with it.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • 2
    Your immediate problem is that the alleged XML you show is not well formed; you need whitespace before the 'default-view' attribute-value specification. Longer-term, in order to get good results on Stack Overflow you need to ask a clear question, not just grammatically but substantively. – C. M. Sperberg-McQueen Apr 01 '15 at 15:45

1 Answers1

1

What's wrong with your XML is that it is not well-formed.

That is, it does not meet the basic requirements for being XML. Then, per your comment about an error indicating that collection-list cannot be found, your XML is also not valid. That is, it does not meet the additional requirements specified by a schema. You might want to read more about Well-formed vs Valid XML in general.

Below, I will help you with each category of problems in your particular case. I will use Xerces-J error message to be concrete, but similar errors will be issued by any conformant XML parser.

Well-Formed

[Fatal Error] try.xml:9:31: Element type "member" must be followed by either attribute specifications, ">" or "/>".

  • Explanation: Parser cannot complete processing of member, indicating a problem while parsing the attributes (before getting to the end of the opening tag as marked by > or />.
  • Resolution: Add whitespace before the default-view="tableview" attribute.

[Fatal Error] try.xml:12:5: The element type "xs:collection" must be terminated by the matching end-tag "</xs:collection>".

  • Explanation: Parser sees no closing </xs:collection> because the namespace prefix is missing on </collection>.
  • Resolution: Add the namespace prefix to the closing collection tag.

Repeat for collection-list.

Other notes:

  • I would reserve the xs namespace prefix for XML Schema elements (although this isn't strictly necessary).
  • I would remove the elementFormDefault attribute from hp:collection-list as it looks like it mistakenly has been copied over from an XSD.
  • I'll assume that the member element is also intended to be in the http://www.hp.com/hpsim7.5.0.0 namespace to simplify demonstrating an XSD for your XML in the next section.

Altogether, then, here is your text turned into well-formed XML:

<?xml version="1.0" encoding="UTF-8" ?>
<hp:collection-list xmlns:hp="http://www.hp.com/hpsim7.5.0.0">    
  <hp:collection name="abc" 
                 type="system" 
                 parent="Systems by Type">
    <hp:member name="All Servers" 
               type="query" 
               display-status="0"
               default-view="tableview" 
               hidden="false"/>
  </hp:collection>
</hp:collection-list>

Valid

Now that you have well-formed XML, you can proceed to attempt to validate it, but when you do, you'll encounter another error:

[Error] try.xml:3:52: cvc-elt.1.a: Cannot find the declaration of element 'hp:collection-list'.

  • Explanation: Parser cannot find an XSD for the elements in this file, starting with the root element, hp:collection-list.
  • Resolution: Hint to the parser where to look for the XSD using xsi:schemaLocation, whose value consists of namespace-location pairs, separated by spaces.

Here is your XML with xsi:schemaLocation hinting the XSD location:

<?xml version="1.0" encoding="UTF-8" ?>
<hp:collection-list xmlns:hp="http://www.hp.com/hpsim7.5.0.0" 
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xsi:schemaLocation="http://www.hp.com/hpsim7.5.0.0 try.xsd">    
  <hp:collection name="abc" 
                 type="system" 
                 parent="Systems by Type">
    <hp:member name="All Servers" 
               type="query" 
               display-status="0"
               default-view="tableview" 
               hidden="false" />
  </hp:collection>
</hp:collection-list>

And here is an XSD that validates the above XML:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://www.hp.com/hpsim7.5.0.0">
  <xs:element name="collection-list">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="collection" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="member">
                <xs:complexType>
                  <xs:sequence/>
                  <xs:attribute name="name"/>
                  <xs:attribute name="type"/>
                  <xs:attribute name="display-status" type="xs:integer"/>
                  <xs:attribute name="default-view"/>
                  <xs:attribute name="hidden" type="xs:boolean"/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="name"/>
            <xs:attribute name="type"/>
            <xs:attribute name="parent"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Thank you guys for looking into the code. I agree the question wasn't right, since I am new to this. Was trying to debug a code written previously. When I try to run it, it gives me the below error : error: XML input is invalid on line 2 near column 57 Error on line 2: cvc-elt.1: Cannot find the declaration of element 'collection-list'. – Sneha Naik Apr 02 '15 at 06:30
  • I have greatly expanded my answer to illustrate how to resolve both the original well-formedness problems and the new validity issue you mentioned in your comment. Hopefully you, or at least future readers at this point, will benefit from the added detail and explanations. – kjhughes Jun 02 '15 at 03:15