xpath 从 jdo 映射中提取字段名称和 "column" 名称

xpath extract field name and "column" name from jdo mapping

第一次处理 xpath 和 XML 数据。我有以下通过一些 Stack Overflow 答案获得的 xpath 查询。下面,我要提取所有的列名

    with t(x) as ( 
    values
    ('<?xml version="1.0" encoding="UTF-8"?>
<mapping>
   <package name="mypackage">
      <class name="mytable">
         <jdbc-class-map type="base" pk-column="id" table="public.mytable" />
         <jdbc-version-ind type="version-number" column="version" />
         <jdbc-class-ind type="myclass" column="jdoclass" />
         <field name="majorVersion">
            <jdbc-field-map type="value" column="majorversion" />
         </field>
         <field name="minorVersion">
            <jdbc-field-map type="value" column="minorversion" />
         </field>
         <field name="patchVersion">
            <jdbc-field-map type="value" column="patchversion" />
         </field>
         <field name="version">
            <jdbc-field-map type="value" column="version0" />
         </field>
         <field name="webAddress">
            <jdbc-field-map type="value" column="webaddress" />
         </field>
      </class>
   </package>
</mapping>'::xml)
    )
    
    select 
        unnest(xpath('./package/class/field/text()', x)) as "fieldname",
        unnest(xpath('./package/class/field/jdbc-field-map/text()', x)) as "columns"
    from t

以上查询returns字段名为空,列为空。我知道 XML 路径有问题。 我希望看到字段名称和列列表

fieldName      columns
--------------------------
majorversion   majorversion
minorversion   minorversion
...

如果您想将 XML 变成“table”,通常使用 xmltable()

会更容易
select info.*
from t
  cross join xmltable('/mapping/package/class/field' passing x
                      columns fieldname text path '@name', 
                              "column"  text path './jdbc-field-map/@column') as info

Online example

我能够通过

获得结果
with myTempTable(myXmlColumn) as (
values ('<?xml version="1.0" encoding="UTF-8"?>
<mapping>
   <package name="mypackage">
      <class name="mytable">
         <jdbc-class-map type="base" pk-column="id" table="public.mytable" />
         <jdbc-version-ind type="version-number" column="version" />
         <jdbc-class-ind type="myclass" column="jdoclass" />
         <field name="majorVersion">
            <jdbc-field-map type="value" column="majorversion" />
         </field>
         <field name="minorVersion">
            <jdbc-field-map type="value" column="minorversion" />
         </field>
         <field name="patchVersion">
            <jdbc-field-map type="value" column="patchversion" />
         </field>
         <field name="version">
            <jdbc-field-map type="value" column="version0" />
         </field>
         <field name="webAddress">
            <jdbc-field-map type="value" column="webaddress" />
         </field>
      </class>
   </package>
</mapping>'::xml))

SELECT 
    unnest(xpath('//package/class/field/jdbc-field-map/@column', myTempTable.myXmlColumn))::text AS columns,
    unnest(xpath('//package/class/field//@name', myTempTable.myXmlColumn))::text AS fieldName
FROM myTempTable

结果

fieldName      columns
--------------------------
"majorversion"  "majorVersion"
"minorversion"  "minorVersion"
"patchversion"  "patchVersion"
"version0"      "version"
"webaddress"    "webAddress"