Movatterモバイル変換


[0]ホーム

URL:


CN113986957A - XPath-based database query method - Google Patents

XPath-based database query method
Download PDF

Info

Publication number
CN113986957A
CN113986957ACN202111290429.5ACN202111290429ACN113986957ACN 113986957 ACN113986957 ACN 113986957ACN 202111290429 ACN202111290429 ACN 202111290429ACN 113986957 ACN113986957 ACN 113986957A
Authority
CN
China
Prior art keywords
query
xpath
many
database
data
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202111290429.5A
Other languages
Chinese (zh)
Inventor
麦天骥
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
BEIJING LEDICT TECHNOLOGY CO LTD
Original Assignee
BEIJING LEDICT TECHNOLOGY CO LTD
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by BEIJING LEDICT TECHNOLOGY CO LTDfiledCriticalBEIJING LEDICT TECHNOLOGY CO LTD
Priority to CN202111290429.5ApriorityCriticalpatent/CN113986957A/en
Publication of CN113986957ApublicationCriticalpatent/CN113986957A/en
Pendinglegal-statusCriticalCurrent

Links

Images

Classifications

Landscapes

Abstract

The invention discloses a database query method based on XPath, which comprises the following steps: analyzing XPath sentences, translating the XPath sentences into SQL sentences, then inquiring the database, wherein the table and the table of the database are associated and mapped by one-to-one, one-to-many and many-to-many mapping relations, when the table is one-to-one and one-to-many, the structure of the data in the table can be regarded as tree-shaped and is equal to the data structure in the XML, and for the data which is the same tree-shaped structure, no matter whether XML documents or the database is used as a storage medium, the query is carried out by using a unified query language. The invention solves the problems of low efficiency and heavy burden of the existing database resource query.

Description

XPath-based database query method
Technical Field
The invention relates to the technical field of data processing, in particular to a database query method based on XPath.
Background
Existing relational databases are already very sophisticated applications, and the field-based approach can provide accurate data definition and control, but has the disadvantage that their data structure is relatively fixed. Once the data structure needs to be extended, it must be implemented by modifying the database table structure, the extent of the modification may involve relationships with other tables and all the data in the data table. And XMl takes a hierarchical tree structure as a storage framework, contains data through elements (elements) and attributes (attributes), and guarantees the correctness of the data structure through XMLSchema. In this way, the data structure has dynamic characteristics, and the XML node can be changed at any time as long as the XL Schema definition is met. This has prompted XML databases to become a hotspot for academic research.
With the extensible markup language XML, the standard for data representation and data exchange on lnternet has become. How to effectively manage and quickly query the XML documents is also the focus of research, namely, the so-called XML database.
XPath is a lightweight XML query language, and is the basis for other XML query languages such as XQuery, XSLT, to locate textual data, elements, attributes, and other information in an XL document by carrying search path expressions.
There are currently a number of approaches to the implementation of XPath: a native XML database based on a network hierarchy model, an extended implementation for a relational database, database middleware, and the like. The extended implementation scheme for the relational database well combines the advantages of the relational model and the semi-structured hierarchical model, but the XPath implementation of popular relational databases such as SQL Server depends on the matching of character strings and the LIKE, and because the efficiency of a computer in character string operation is lower than that of integer operation, some performance problems still exist.
Usually, XML documents are used to store data, and we will consider how to uniformly convert a standard query SQL of a relational database into a standard query XPath of XML data. Since the use of relational data query criteria is relatively extensive. XPath is a mechanism for querying data nodes in an XML document tree proposed by W3C, and is widely applied to XML query language XQuery. However, because the tree data model of XML and the data model of the two-dimensional table of the relational database are very different, and the supported characteristics of XPath are very many, how to convert SQL into equivalent XPath faces a great challenge.
Disclosure of Invention
Therefore, the invention provides an XPath-based database query method, which is used for solving the problems of low efficiency and heavy burden of database resource query in the prior art.
In order to achieve the above purpose, the invention provides the following technical scheme:
the invention discloses a database query method based on XPath, which comprises the following steps:
analyzing XPath sentences, translating the XPath sentences into SQL sentences, then inquiring the database, wherein the table and the table of the database are associated and mapped by one-to-one, one-to-many and many-to-many mapping relations, when the table is one-to-one and one-to-many, the structure of the data in the table can be regarded as tree-shaped and is equal to the data structure in the XML, and for the data which is the same tree-shaped structure, no matter whether XML documents or the database is used as a storage medium, the query is carried out by using a unified query language.
Further, the method comprises: the method comprises the steps of single-table query, two-table query and multi-table query, wherein the single-table query corresponds to one-to-one mapping relation, the single-table query serves as a root node in an xpath, and a query field serves as an attribute of the node in the xpath.
Further, in the single-table query, when a field under table _ a is queried, the field can be written as/table _ a/@ a, which is equivalent to sql statement select a from table _ a; when a condition needs to be added to a query, an xpath predicate may be added to the node corresponding to the table name.
Furthermore, the two tables are inquired for a mapping relation corresponding to one-to-many, and the two tables are table _ a and table _ b, wherein table _ a is a parent table of table _ b, and table _ a is written as a parent node of table _ b.
Further, in the two-table query, when querying the b field of table _ b corresponding to table _ a, the b field can be written as/table _ a/table _ b/@ b, and if there is another condition, an xpath predicate can be added at the corresponding table name.
Furthermore, the multi-table query corresponds to a many-to-many mapping relationship, and when table _ b and another table are in a one-to-many relationship, the table can be regarded as a sub-level node of the table _ b node in the XML document, and XPath is used for query.
Further, in the multi-table query process, for more than two tables, as long as the tables are in a child-parent table relationship, the tables may be arranged according to child-parent nodes in an xpath, and predicates are added at corresponding table names.
The invention has the following advantages:
the invention discloses a query method for a database based on XPath, which is used for carrying out association mapping between tables in the database, and for data which is also in a tree structure, a unified query language can be used for querying no matter XML documents or databases are used as storage media, so that the development efficiency is improved, and the workload of developers is reduced.
Drawings
In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below. It should be apparent that the drawings in the following description are merely exemplary, and that other embodiments can be derived from the drawings provided by those of ordinary skill in the art without inventive effort.
The structures, ratios, sizes, and the like shown in the present specification are only used for matching with the contents disclosed in the specification, so as to be understood and read by those skilled in the art, and are not used to limit the conditions that the present invention can be implemented, so that the present invention has no technical significance, and any structural modifications, changes in the ratio relationship, or adjustments of the sizes, without affecting the effects and the achievable by the present invention, should still fall within the range that the technical contents disclosed in the present invention can cover.
FIG. 1 is a flowchart of a method for querying a database based on XPath according to an embodiment of the present invention;
Detailed Description
The present invention is described in terms of particular embodiments, other advantages and features of the invention will become apparent to those skilled in the art from the following disclosure, and it is to be understood that the described embodiments are merely exemplary of the invention and that it is not intended to limit the invention to the particular embodiments disclosed. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
Examples
The embodiment discloses a database query method based on XPath, which comprises the following steps:
analyzing XPath sentences, translating the XPath sentences into SQL sentences, then inquiring the database, wherein the table and the table of the database are associated and mapped by one-to-one, one-to-many and many-to-many mapping relations, when the table is one-to-one and one-to-many, the structure of the data in the table can be regarded as tree-shaped and is equal to the data structure in the XML, and for the data which is the same tree-shaped structure, no matter whether XML documents or the database is used as a storage medium, the query is carried out by using a unified query language.
The method comprises the following steps: the method comprises the steps of single-table query, two-table query and multi-table query, wherein the single-table query corresponds to one-to-one mapping relation, the single-table query serves as a root node in an xpath, and a query field serves as an attribute of the node in the xpath. When looking up the a field under table _ a, can write/table _ a/@ a, equivalent to sql statement select a from table _ a; when a condition needs to be added to the query, an xpath predicate, such as/table _ a [ @ b ═ 1']/@ a, can be added to the node corresponding to the table name, which is equivalent to the sql statement select a from table _ a where b ═ 1'.
The specific process is as follows:
newly creating a table _ a, wherein the table _ a has fields a _ id, a, b and c, the primary key is a _ id, and the table building statement is as follows:
CREATE TABLE`table_a`(
`a_id`int NOT NULL,
`a`varchar(255)NULL,
`b`varchar(255)NULL,
`c`varchar(255)NULL,
PRIMARY KEY(`a_id`)
)
referring to Table 1, two pieces of data are inserted in the table
TABLE 1
a_idabC
1attrA1attrB1attrC1
2attrA2attrB2attrC2
Table _ a may be queried using sql to return the full amount of data;
SELECT*FROM`table_a`;
each piece of data in the table is considered as a node of an XML document, with the table name as the node name and the fields of the table as the attributes of the node. Newly building an XML document doc _ a with root node as root, and adding corresponding nodes as follows:
Figure BDA0003334515390000051
the XML document above is queried using XPath// table _ a, returning all nodes named "table _ a".
If we want to look up the b field of table _ a from the primary key, we write sql with where, for example, as follows:
SELECT b FROM`table_a`where a_id=1
when querying XML, XPath is as follows:
//table_a[@a_id=1]/@b。
at this point, both SQL and XPath return values are "attrB 1". It can be seen that for a single-fragment query of a single table, the expression capabilities of XPath and SQL are similar, and the predicate of the where statement and XPath in SQL are equivalent. XPath "// table _ a [ @ a _ id ═ 1]/@ b" can be translated into SQL "SELECT b FROM table _ a where a _ id ═ 1" to query the table.
The two tables are used for inquiring the mapping relation corresponding to one-to-many, and the two tables are table _ a and table _ b, wherein table _ a is a parent table of table _ b, and table _ a is written as a parent node of table _ b. When querying the b field of table _ b corresponding to table _ a, can be written as/table _ a/table _ b/@ b. If there are other conditions, xpath predicates can be added at the corresponding table names, such as/table _ a [ @ b ═ 1 ']/table _ b [ @ c ═ 2' ]/@ b, equivalent to the sql statement select b.b from table _ a a, table _ b where a.b ═ 1'and b.c ═ 2' and a.id ═ b.a _ id.
The specific process of the two-table query is as follows:
and newly establishing a table _ b, wherein the table _ b has fields b _ id, a, b and c, and the primary key is b _ id. table _ a has a one-to-many relationship with the table, and a _ id of table _ b stores primary key a _ id of table _ a.
CREATE TABLE`table_b`(
`b_id`int(11)NOT NULL,
`a_id`int(11)NOT NULL,
`a`varchar(255)DEFAULT NULL,
`b`varchar(255)DEFAULT NULL,
`c`varchar(255)DEFAULT NULL,
PRIMARY KEY(`b_id`)
)
Referring to Table 2, data is inserted into table _ b
TABLE 2
Figure BDA0003334515390000061
Figure BDA0003334515390000071
Two pieces of data with b _ id of 1 and 2 correspond to the first piece of data of table _ a, and one piece of data with b _ id of 3 corresponds to the second piece of data of table _ a. At this time, each piece of data of table _ a corresponds to zero, one or more pieces of data of table _ b, is a tree-shaped data structure, and can be equivalently regarded as the following XML document doc _ b:
Figure BDA0003334515390000072
if the value of table _ b table a field needs to be queried, SQL is used as follows
Figure BDA0003334515390000073
Figure BDA0003334515390000081
The equivalent can be seen as querying from XML document doc _ b using XPath// table _ a [ @ a ═ attrA1"]/table _ b [ @ b ═ b _ b1'and @ c ═ b _ c1' ]/@ a.
The multi-table query corresponds to a many-to-many mapping relation, when table _ b and other tables are in a one-to-many relation, the table can be regarded as a sub-level node of the table _ b node in the XML document, and XPath is used for query. In the multi-table query process, for more than two tables, as long as the tables are in a child-parent table relationship, the tables can be arranged according to child-parent nodes in the xpath, and predicates are added at corresponding table names. Compared with SQL, when single field query is carried out, multi-stage XPath is more convenient to write than SQL, data corresponding relation can be intuitively embodied, and where statements in SQL can be written under corresponding table names by using the writing method of XPath predicates. For those familiar with data but not with SQL, it is relatively simpler to use XPath syntax, especially if tables have multiple child-parent relationships, the deeper the hierarchy the simpler it is to write compared to SQL.
The method for querying the database based on XPath disclosed by the embodiment carries out association mapping between the table and the table in the database, and can query the data which are both in a tree structure by using a unified query language no matter using an XML document or using the database as a storage medium, so that the development efficiency is improved, and the workload of developers is reduced.
Although the invention has been described in detail above with reference to a general description and specific examples, it will be apparent to one skilled in the art that modifications or improvements may be made thereto based on the invention. Accordingly, such modifications and improvements are intended to be within the scope of the invention as claimed.

Claims (7)

1. A method for querying a database based on XPath is characterized in that the method comprises the following steps:
analyzing XPath sentences, translating the XPath sentences into SQL sentences, then inquiring the database, wherein the table and the table of the database are associated and mapped by one-to-one, one-to-many and many-to-many mapping relations, when the table is one-to-one and one-to-many, the structure of the data in the table can be regarded as tree-shaped and is equal to the data structure in the XML, and for the data which is the same tree-shaped structure, no matter whether XML documents or the database is used as a storage medium, the query is carried out by using a unified query language.
2. A method for database query based on XPath as claimed in claim 1, wherein said method comprises: the method comprises the steps of single-table query, two-table query and multi-table query, wherein the single-table query corresponds to one-to-one mapping relation, the single-table query serves as a root node in an xpath, and a query field serves as an attribute of the node in the xpath.
3. The method as claimed in claim 2, wherein in the single table query, when looking up the a field under table _ a, it can be written as/table _ a/@ a, which is equivalent to the sql statement select a from table _ a; when a condition needs to be added to a query, an xpath predicate may be added to the node corresponding to the table name.
4. The method as claimed in claim 2, wherein the two table queries correspond to one-to-many mapping relationship, and the two tables are table _ a and table _ b, and table _ a is a parent table of table _ b, and table _ a is written as a parent node of table _ b.
5. The method as claimed in claim 4, wherein in the two-table query, when querying the b field of table _ b corresponding to table _ a, the b field can be written as/table _ a/table _ b/@ b, if there is another condition, the XPath predicate can be added at the corresponding table name.
6. The method as claimed in claim 2, wherein the multi-table query corresponds to many-to-many mapping relationship, and when table _ b is in one-to-many relationship with another table, the table can be regarded as a sub-level node of table _ b node in XML document, and the query is performed by using XPath.
7. The method as claimed in claim 2, wherein in the multi-table query process, for more than two tables, as long as the tables have a child-parent table relationship, the tables can be arranged according to child-parent nodes in XPath, and predicates are added at corresponding table names.
CN202111290429.5A2021-11-022021-11-02XPath-based database query methodPendingCN113986957A (en)

Priority Applications (1)

Application NumberPriority DateFiling DateTitle
CN202111290429.5ACN113986957A (en)2021-11-022021-11-02XPath-based database query method

Applications Claiming Priority (1)

Application NumberPriority DateFiling DateTitle
CN202111290429.5ACN113986957A (en)2021-11-022021-11-02XPath-based database query method

Publications (1)

Publication NumberPublication Date
CN113986957Atrue CN113986957A (en)2022-01-28

Family

ID=79745882

Family Applications (1)

Application NumberTitlePriority DateFiling Date
CN202111290429.5APendingCN113986957A (en)2021-11-022021-11-02XPath-based database query method

Country Status (1)

CountryLink
CN (1)CN113986957A (en)

Citations (6)

* Cited by examiner, † Cited by third party
Publication numberPriority datePublication dateAssigneeTitle
CN1561497A (en)*2001-09-282005-01-05甲骨文国际公司Providing a consistent hierarchical abstraction of relational data
CN1560763A (en)*2004-02-192005-01-05北京大学 A Method for Translating Extensible Markup Language Path Query into Structure Query
KR20070047470A (en)*2005-11-022007-05-07정민경 Query Processing System and Method for Searching XML Documents in Relational Databases
CN101350028A (en)*2008-07-102009-01-21西安中电商务信息技术有限公司XML data XPath search method based on SQL structured search language
US20110093486A1 (en)*2009-10-152011-04-21Institute For Information IndustryData query method, data query system and computer readable and writable recording medium
CN110110150A (en)*2018-01-042019-08-09北大医疗信息技术有限公司Read method, reading device, computer equipment and the storage medium of XML data

Patent Citations (6)

* Cited by examiner, † Cited by third party
Publication numberPriority datePublication dateAssigneeTitle
CN1561497A (en)*2001-09-282005-01-05甲骨文国际公司Providing a consistent hierarchical abstraction of relational data
CN1560763A (en)*2004-02-192005-01-05北京大学 A Method for Translating Extensible Markup Language Path Query into Structure Query
KR20070047470A (en)*2005-11-022007-05-07정민경 Query Processing System and Method for Searching XML Documents in Relational Databases
CN101350028A (en)*2008-07-102009-01-21西安中电商务信息技术有限公司XML data XPath search method based on SQL structured search language
US20110093486A1 (en)*2009-10-152011-04-21Institute For Information IndustryData query method, data query system and computer readable and writable recording medium
CN110110150A (en)*2018-01-042019-08-09北大医疗信息技术有限公司Read method, reading device, computer equipment and the storage medium of XML data

Similar Documents

PublicationPublication DateTitle
SchöningTamino-a DBMS designed for XML
US6836778B2 (en)Techniques for changing XML content in a relational database
US9330149B2 (en)Techniques for query and DML over relational tables using spreadsheet applications
US7499915B2 (en)Index for accessing XML data
US7644066B2 (en)Techniques of efficient XML meta-data query using XML table index
US6721727B2 (en)XML documents stored as column data
US7120645B2 (en)Techniques for rewriting XML queries directed to relational database constructs
CA2522309C (en)Retaining hierarchical information in mapping between xml documents and relational data
US20100169354A1 (en)Indexing Mechanism for Efficient Node-Aware Full-Text Search Over XML
US20050228786A1 (en)Index maintenance for operations involving indexed XML data
CA2421214C (en)Method and apparatus for xml data storage, query rewrites, visualization, mapping and referencing
US20100185683A1 (en)Indexing Strategy With Improved DML Performance and Space Usage for Node-Aware Full-Text Search Over XML
US7849106B1 (en)Efficient mechanism to support user defined resource metadata in a database repository
CN113986957A (en)XPath-based database query method
Lin et al.Object-oriented mediator queries to XML data
Lin et al.Adaptive data mediation over XML data
CN112860947A (en)RDF construction method based on XML indirect mapping
CA2561734C (en)Index for accessing xml data
Yang et al.Efficiently mapping integrity constraints from relational database to XML document1
El Alami et al.Schema and Data Migration of a Relational Database RDB to the Extensible Markup Language XML
Papamarkos et al.Xml Databases
Zhang et al.XR-lock: locking XML data for efficient concurrency control
Al HamadCatalog-based Conversion from Relational Database into XML Scheme (XSD)
Pardede et al.Preserving composition in XML object relational storage
de Brum SaccolUSE OF DATABASES FOR STORING XML DOCUMENTS

Legal Events

DateCodeTitleDescription
PB01Publication
PB01Publication
SE01Entry into force of request for substantive examination
SE01Entry into force of request for substantive examination

[8]ページ先頭

©2009-2025 Movatter.jp