XPath-based database query methodTechnical 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_id | a | b | C |
| 1 | attrA1 | attrB1 | attrC1 |
| 2 | attrA2 | attrB2 | attrC2 |
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:
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
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:
if the value of table _ b table a field needs to be queried, SQL is used as follows
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.