search
HomeBackend DevelopmentXML/RSS TutorialDetailed explanation of code examples using regular expressions for xml data validation

xml Schema is a data definition file that defines XML, with .xsd as the file extension. It can also be used to define a class of XML files.

Usually, some data with special meaning cannot be clearly described by the system's preset data structure (type).
The XML Schema specification states that simple types can be restricted through facets, thereby generating some new atomic types (Atomic types).
Facet has pattern, enumeration, etc.;
What I want to say here is that one of the very useful ones is:
pattern+ regular expression language (regular exPRession language)
Combined with the power of regular expressions function, you can describe some complex data structures

Examples can be verified through xmlspy, xmlwrite, or js/vbs, etc. The following is an example of js verification (requires msxml4.0 support)


Information about defining XML Schema can be found in Part 1 of the W3C's XML Schema specification. For information about built-in data types and the limitations of their availability, check Part 2 of the XML Schema specification. For a brief summary of these two parts of the XML Schema specification, see W3C Primer on XML Schema.

For regular expressions, you can go to http://www.regexlib.com/ to see

examples:

/*** examples.xml ***/
<?xml version="1.0" encoding="gb2312"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="examples.xsd">
    <user>
  <name>test</name>
  <email>moonpiazza@hotmail.com</email>
  <ip>127.0.0.1</ip>
  <color>#000000</color>
    </user>
    <user>
  <name>guest</name>
  <email>guest@371.net</email>
  <ip>202.102.224.25</ip>
  <color>#FFFFFF</color>
    </user>    
</root>


/*** examples.xsd ***/
<?xml version="1.0" encoding="gb2312"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="root" type="Root"/>

<xsd:complexType name="Root">
 <xsd:sequence>
  <xsd:element name="user"  type="User" minOccurs="0" maxOccurs="unbounded" />
 </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="User">
 <xsd:sequence>
  <xsd:element name="name" type="xsd:string"/>
  <xsd:element name="email" type="Email" />
  <xsd:element name="ip" type="IP" />
  <xsd:element name="color" type="Color" />
 </xsd:sequence>
</xsd:complexType>

<xsd:simpleType name="Email">
 <xsd:restriction base="xsd:string">
  <xsd:pattern value="([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)"/>
 </xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="IP">
 <xsd:restriction base="xsd:string">
  <xsd:pattern value="(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.
  (25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.
  (25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])"/>
 </xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="Color">
 <xsd:restriction base="xsd:string">
  <xsd:pattern value="#?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?"/>
 </xsd:restriction>
</xsd:simpleType>

</xsd:schema>


/*** examples.htm ***/
<SCRIPT LANGUAGE="javaScript">
function validate()
{
 var oXML ;
 var nParseError;
 var sReturnVal;

 oXML = new ActiveXObject("MSXML2.DOMDocument.4.0") ;
 oXML.async = false ;
 oXML.validateOnParse = true;

 oXML.load("examples.xml") ;

 nParseError = oXML.parseError.errorCode ;
 sReturnVal = "" ;

 if (0 != nParseError)
 {
  //参看书籍教程中parseError对象属性
  sReturnVal = sReturnVal + "代码:" + oXML.parseError.errorCode + "\n" ;
  sReturnVal = sReturnVal + "错误原因:" + oXML.parseError.Reason + "\n" ;
  sReturnVal = sReturnVal + "错误字符串:" + oXML.parseError.srcText + "\n" ;
  sReturnVal = sReturnVal + "错误行号" + oXML.parseError.line + "\n" ;
  sReturnVal = sReturnVal + "错误列数:" + oXML.parseError.linepos + "\n" ;
 }
 else
 {
  sReturnVal = sReturnVal + "验证通过!"
 }

  alert(sReturnVal);
}

function window.onload()
{
 validate();
}
</SCRIPT>

The above is the use of regular expressions Detailed explanation of the code example for expression xml data verification. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Anatomy of an RSS Document: Structure and ElementsThe Anatomy of an RSS Document: Structure and ElementsMay 10, 2025 am 12:23 AM

The structure of an RSS document includes three main elements: 1.: root element, defining the RSS version; 2.: Containing channel information, such as title, link, and description; 3.: Representing specific content entries, including title, link, description, etc.

Understanding RSS Documents: A Comprehensive GuideUnderstanding RSS Documents: A Comprehensive GuideMay 09, 2025 am 12:15 AM

RSS documents are a simple subscription mechanism to publish content updates through XML files. 1. The RSS document structure consists of and elements and contains multiple elements. 2. Use RSS readers to subscribe to the channel and extract information by parsing XML. 3. Advanced usage includes filtering and sorting using the feedparser library. 4. Common errors include XML parsing and encoding issues. XML format and encoding need to be verified during debugging. 5. Performance optimization suggestions include cache RSS documents and asynchronous parsing.

RSS, XML and the Modern Web: A Content Syndication Deep DiveRSS, XML and the Modern Web: A Content Syndication Deep DiveMay 08, 2025 am 12:14 AM

RSS and XML are still important in the modern web. 1.RSS is used to publish and distribute content, and users can subscribe and get updates through the RSS reader. 2. XML is a markup language and supports data storage and exchange, and RSS files are based on XML.

Beyond Basics: Advanced RSS Features Enabled by XMLBeyond Basics: Advanced RSS Features Enabled by XMLMay 07, 2025 am 12:12 AM

RSS enables multimedia content embedding, conditional subscription, and performance and security optimization. 1) Embed multimedia content such as audio and video through tags. 2) Use XML namespace to implement conditional subscriptions, allowing subscribers to filter content based on specific conditions. 3) Optimize the performance and security of RSSFeed through CDATA section and XMLSchema to ensure stability and compliance with standards.

Decoding RSS: An XML Primer for Web DevelopersDecoding RSS: An XML Primer for Web DevelopersMay 06, 2025 am 12:05 AM

RSS is an XML-based format used to publish frequently updated data. As a web developer, understanding RSS can improve content aggregation and automation update capabilities. By learning RSS structure, parsing and generation methods, you will be able to handle RSSfeeds confidently and optimize your web development skills.

JSON vs. XML: Why RSS Chose XMLJSON vs. XML: Why RSS Chose XMLMay 05, 2025 am 12:01 AM

RSS chose XML instead of JSON because: 1) XML's structure and verification capabilities are better than JSON, which is suitable for the needs of RSS complex data structures; 2) XML was supported extensively at that time; 3) Early versions of RSS were based on XML and have become a standard.

RSS: The XML-Based Format ExplainedRSS: The XML-Based Format ExplainedMay 04, 2025 am 12:05 AM

RSS is an XML-based format used to subscribe and read frequently updated content. Its working principle includes two parts: generation and consumption, and using an RSS reader can efficiently obtain information.

Inside the RSS Document: Essential XML Tags and AttributesInside the RSS Document: Essential XML Tags and AttributesMay 03, 2025 am 12:12 AM

The core structure of RSS documents includes XML tags and attributes. The specific parsing and generation steps are as follows: 1. Read XML files, process and tags. 2. Extract,,, etc. tag information. 3. Handle custom tags and attributes to ensure version compatibility. 4. Use cache and asynchronous processing to optimize performance to ensure code readability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools