Updated April 3, 2023
Definition of String in XML
XML Schema defines in a way what an XML document contains, therefore, XSD defines the string so, it can be defined as a value that contains character strings also has Unicode character given by XML and represented using the type xs: string, while this type has a white space character and maintained by the processor as data types, can pick characters, line feeds as well, for instance, multiple tabs and spaces are preserved during display with the namespace as http://www.w3.org/2001/XMLschema along with the qualifier xs to identify the schema elements and its string types.
How to Define String in XML?
A string can be defined as follows:
<xs : Element name =”name” type = “xs:string”/>
Here element name = name given in the xml tag
How to Initialize String in XML?
A string is initialized in the Element name with the type ‘string’. For example:
If the XSD is:
<xs : element name =”Office_Location” type = “xs:string”/>
Then Sample Xml is
<Office_Location>
34, St.George Street
</Office_Location>
It returns the string in the element name, if it is an integer then it returns a value. If the type doesn’t have any content then it could be declared as a string, not as any data types. Also that when defining with <xs:> it is mandatory to look out the valid data values using fixed and default.
Using fixed:
The value with fixed says the specified value in the XSD document can only be used.
<xs : element name =”Child_name” type = “xs:string”
Fixed=” starts with A”/>
Using Default:
<xs : element name =”Office_Location” type = “xs:string”/>
Default=” Unknown”:/>
String Functions in XML with Examples
There are three types of Function resources used, which are:
1. <xs: token>: This type has one token with nill white spaces and it is derived from normalized string with the value lesser than this.
<xs:sequence>
<xs:element name="Tokenname" type="xs:token"
maxOccurs="unbounded"/>
</xs:sequence>
2. <xs:normalizedString>: This type of string has a white space which is replaced by a single space and derived from xs:string type. They are considered to be a pre-defined datatype.
<xs:sequence>
<xs:element name="NorString" type="xs:normalizedString"
maxOccurs="unbounded"/>
</xs:sequence>
3. <xs: id>: This is used when a string uses ID attributes in schemas which is used as unique identifiers.
<xs:simpleType name="Ident" id="ID">
<xs:restriction base="xs:NCName"/>
</xs:simpleType>
Other types are an entity. language, name, NMtokens, language, IDREF which are not used much. The following are the examples with the Schema file where the respective string is declared to explain the datatype of the elements used in the XML file. Let’s start with a simple example.
Example #1
See I have created two files string.xml and string.xsd in the same folder and after creating XSD file include the namespace in XML with the .xsd file. Now execute xml file.
string.xml
<?xml version="1.0" encoding="UTF-8"?>
<contact
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" SchemaLocation="string.xsd">
Lawrence Evster
</contact>
string.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="contact" type="xs:string"/>
</xs:schema>
Output:
Example #2
Now let’s see full XSD with a string that goes along with XML file. Here with the strings, we added cardinality attributes min and max.
College Schema.xsd
<?xml version="1.0" encoding="UTF-8"?><xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="College"
targetNamespace="College" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="Faculty" type="FacultyType"/>
<xs:element name="Result">
<xs:complexType>
<xs:sequence>
<xs:element ref="College" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="College">
<xs:complexType>
<xs:sequence>
<xs:choice maxOccurs="unbounded">
<xs:element name="Faculty" type="FacultyType"/>
<xs:element name="Principal" type="FacultyType"/>
</xs:choice>
<xs:element name="Dept" type="xs:string" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="noOfWorkers" type="xs:int"/>
</xs:complexType>
</xs:element>
<xs:complexType name="FacultyType">
<xs:sequence>
<xs:element name="FName" type="xs:string" minOccurs="0"/>
<xs:element name="LName" type="xs:string" minOccurs="0"/>
<xs:element name="FfName" type="xs:string" minOccurs="0"/>
<xs:element name="Idno" type="xs:string"/>
<xs:element name="YearsOfExperience" type="xs:int" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
co.xml
<?xml version="1.0" encoding="utf-8"?>
<Result xmlns="College" xsi:schemaLocation="College schema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<College noOfWorkers="4420">
<Principal>
<LName>string</LName>
<Idno>string</Idno>
</Principal>
<Faculty>
<FName>string</FName>
<LName>string</LName>
<FfName>string</FfName>
<Idno>string</Idno>
</Faculty>
<Dept>string</Dept>
</College>
</Result>
Result:
Output:
Example #3
odrs.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Purchaseorder">
<xs:complexType>
<xs:sequence>
<xs:element name="client">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="PostalAddress" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="product">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="prod">
<xs:complexType>
<xs:attribute name="pid" type="xs:unsignedShort" use="required" />
<xs:attribute name="qty" type="xs:unsignedByte" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
odr.xml
<?xml version="1.0" encoding="UTF-8"?>
<Purchaseorder xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
SchemaLocation="odrs.xsd">
<client>
<name> Anto Sukesh</name>
<PostalAddress>
13 Grwtwn road,
greenland town,
Texas
</PostalAddress>
</client>
<product>
<prod pid="3244" qty="3" />
<prod pid="1532" qty="5" />
</product>
</Purchaseorder>
Result:
Output:
Example #4 – Using Token Type
ama.xml
<?xml version="1.0" encoding="UTF-8"?>
<Amazon xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
SchemaLocation="ama.xsd">
<product id="p3487611190" available="true">
<barcode>2342222221</barcode>
<title lang="en">Amazon India</title>
<subprod id="book">
<bname></bname>
<date>2009-10-20</date>
</subprod>
<author id="Anoop">
<cname>Anoop</cname>
<dob>1940-02-04</dob>
<qualification>not</qualification>
</author>
</product>
</Amazon>
ama.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="bname" type="xs:token"/>
<xs:element name="qualification" type="xs:token"/>
<xs:element name="dob" type="xs:date"/>
<xs:element name="barcode" type="xs:NMTOKEN"/>
<xs:attribute name="id" type="xs:ID"/>
<xs:attribute name="available" type="xs:boolean"/>
<xs:attribute name="lang" type="xs:language"/>
<xs:element name="title">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:token">
<xs:attribute ref="lang"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="Amazon">
<xs:complexType>
<xs:sequence>
<xs:element ref="product" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="subprod">
<xs:complexType>
<xs:sequence>
<xs:element ref="bname"/>
<xs:element ref="dob"/>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:complexType>
</xs:element>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element ref="barcode"/>
<xs:element ref="title"/>
<xs:element ref="author" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="author" minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute ref="id"/>
<xs:attribute ref="available"/>
</xs:complexType>
</xs:element>
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<xs:element ref="cname"/>
<xs:element ref="dob"/>
<xs:element ref="qualification"/>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:complexType>
</xs:element>
</xs:schema>
Output:
Example #5
Let’s see how xml strings are validated using class and methods in java. The XML file would look like this:
new.xml
<Doctors>
<Doctor id="211">
<dname>Evangeline Mark</dname>
<Specialization>Paediatrician</Specialization>
</Doctor>
<Doctor id="321">
<dname>Anoop JAgadhish</dname>
<Specialization>Physician</Specialization>
</Doctor>
</Doctors>
Stringxml.java
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
public class Stringxml
{
public static void main(String[] args)
{
final String Str = " <Doctors>" +
" <Doctor id=\"211\">" +
" <dname>Evangeline Mark</dname>" +
" <Specialization>Paediatrician</Specialization>" +
" </Doctor>" +
" <Doctor id=\"321\">" +
" <dname>Anoop JAgadhish</dname>" +
" <Specialization>Physician</Specialization>" +
" </Doctor>" +
"</Doctors>";
Document dt = convertStringToXMLDocument( Str );
System.out.println(dt.getFirstChild().getNodeName());
}
private static Document convertStringToXMLDocument(String xstr)
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try
{
builder = factory.newDocumentBuilder();
Document dc = builder.parse(new InputSource(new StringReader(xstr)));
return dc;
}
catch (Exception ee)
{
ee.printStackTrace();
}
return null;
}
}
Output:
Conclusion
Therefore, we have seen how to use a string with the help of XML Schema as they inherit a database which defines data. Here we explained the basics of String and their role in XML which is quite beneficial in hierarchical data structures also done with real-time examples of how the strings are extracted from the schema file. They provide an ability to describe element type with a string like a string to be started from upper case / lower case or any other ranges.
Recommended Articles
This is a guide to String in XML. Here we also discuss the Introduction and how to initialize string? along with string functions in xml with examples. You may also have a look at the following articles to learn more –