EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials XML Tutorial String in XML
 

String in XML

Priya Pedamkar
Article byPriya Pedamkar

Updated April 3, 2023

String in XML

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

String in XML-1.1

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:

String in XML-1.2

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:

Output-1.3

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:

Output-1.4

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:

Output-1.5

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 –

  1. XML with CSS
  2. XML Versions
  3. XML Features
  4. XML Commands

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW