Как использовать xpath для получения родительского атрибута в XML-файле в Postgres SQL - PullRequest
0 голосов
/ 18 сентября 2018

У меня есть XML-файл ниже, и я использую код postgresql для вставки его значений в таблицу со схемой (countryname, ContinentName).Он выбирает значения ContinentName в один столбец, а все значения названия страны - в другой столбец.В действительности, некоторые названия стран имеют пустое название континента.Я хочу, чтобы он выбрал значения названия страны и их соответствующих родительских ContinentName.Мне действительно нужно, чтобы кто-то показал мне, как это сделать.

CREATE TABLE XMLTEST1 (Name XML); 
INSERT INTO XMLTEST1 VALUES
   ('
    <?xml version="1.0" encoding="ISO-8859-1"?>
<Continents>
  <Continent>
    <ContinentName>Europe</ContinentName>
    <Country>
      <CountryName>Austria</CountryName>
      <CountryCode>AT</CountryCode>
      <CountryCapital>Vienna</CountryCapital>
      <Population>8316487</Population>
      <Subdivision>Austria is divided into nine Bundesländer, or simply Länder (states; sing. Land).</Subdivision>
      <State type="state">
        <StateName>Burgenland</StateName>
        <StateCode>BU</StateCode>
        <StateCapital>Eisenstadt</StateCapital>
      </State>
      <State type="state">
        <StateName>Kärnten</StateName>
        <EnglishStateName>Carinthia</EnglishStateName>
        <StateCode>KA</StateCode>
        <StateCapital>Klagenfurt</StateCapital>
      </State>
      <State type="state">
        <StateName>Niederösterreich</StateName>
        <EnglishStateName>Lower Austria</EnglishStateName>
        <StateCode>NO</StateCode>
        <StateCapital>St. Pölten</StateCapital>
      </State>
      <State type="state">
        <StateName>Oberösterreich</StateName>
        <EnglishStateName>Upper Austria</EnglishStateName>
        <StateCode>OO</StateCode>
        <StateCapital>Linz</StateCapital>
      </State>
      <State type="state">
        <StateName>Salzburg</StateName>
        <StateCode>SZ</StateCode>
        <StateCapital>Salzburg</StateCapital>
      </State>
      <State type="state">
        <StateName>Steiermark</StateName>
        <EnglishStateName>Styria</EnglishStateName>
        <StateCode>ST</StateCode>
        <StateCapital>Graz</StateCapital>
      </State>
      <State type="state">
        <StateName>Tirol</StateName>
        <EnglishStateName>Tyrol</EnglishStateName>
        <StateCode>TR</StateCode>
        <StateCapital>Innsbruck</StateCapital>
      </State>
      <State type="state">
        <StateName>Vorarlberg</StateName>
        <StateCode>VO</StateCode>
        <StateCapital>Bregenz</StateCapital>
      </State>
      <State type="state">
        <StateName>Wien</StateName>
        <EnglishStateName>Vienna</EnglishStateName>
        <StateCode>WI</StateCode>
        <StateCapital>Vienna</StateCapital>
      </State>
    </Country>
    <Country>
      <CountryName>Belgium</CountryName>
      <CountryCode>BE</CountryCode>
      <CountryCapital>Bruxelles</CountryCapital>
      <Population>10584534</Population>
      <Subdivision>Belgium is divided into ten provinces (Flemish: provincies) and one capital region (Flemish: hoofdstedelijke gewest; French: région capitale).</Subdivision>
      <State type="province">
        <StateName>Antwerp</StateName>
        <StateCode>AN</StateCode>
        <StateCapital>Antwerp</StateCapital>
        <RegionName>Vlaanderen </RegionName>
      </State>
      <State type="province">
        <StateName>Limburg</StateName>
        <StateCode>LI</StateCode>
        <StateCapital>Hasselt</StateCapital>
        <RegionName>Vlaanderen </RegionName>
      </State>
      <State type="province">
        <StateName>Vlaams Brabant</StateName>
        <EnglishStateName>Flemish Brabant</EnglishStateName>
        <StateCode>VB</StateCode>
        <StateCapital>Leuven</StateCapital>
        <RegionName>Vlaanderen </RegionName>
      </State>
      <State type="province">
        <StateName>Oost-Vlaanderen</StateName>
        <EnglishStateName>East Flanders</EnglishStateName>
        <StateCode>OV</StateCode>
        <StateCapital>Leuven</StateCapital>
        <RegionName>Vlaanderen </RegionName>
      </State>
      <State type="province">
        <StateName>West-Vlaanderen</StateName> 
        <EnglishStateName>West Flanders</EnglishStateName>
        <StateCode>WV</StateCode>
        <StateCapital>Brugge</StateCapital>
        <RegionName>Vlaanderen </RegionName>
      </State>
      <State type="capital region">
        <StateName>Bruxelles</StateName> 
        <EnglishStateName>Brussels</EnglishStateName>
        <StateCode>BU</StateCode>
        <StateCapital>Bruxelles</StateCapital>
        <RegionName>Bruxelles</RegionName>
      </State>
      <State type="province">
        <StateName>Hainaut</StateName>
        <StateCode>HT</StateCode>
        <StateCapital>Mons</StateCapital>
        <RegionName>Wallonie</RegionName>
      </State>
      <State type="province">
        <StateName>Brabant wallon</StateName>
        <EnglishStateName>Walloon Brabant</EnglishStateName>
        <StateCode>BW</StateCode>
        <StateCapital>Wavre</StateCapital>
        <RegionName>Wallonie</RegionName>
      </State>
      <State type="province">
        <StateName>Namur</StateName>
        <StateCode>NA</StateCode>
        <StateCapital>Namur</StateCapital>
        <RegionName>Wallonie</RegionName>
      </State>
      <State type="province">
        <StateName>Liège</StateName>
        <StateCode>LG</StateCode>
        <StateCapital>Liège</StateCapital>
        <RegionName>Wallonie</RegionName>
      </State>
      <State type="province">
        <StateName>Luxembourg</StateName>
        <StateCode>LX</StateCode>
        <StateCapital>Arlon</StateCapital>
        <RegionName>Wallonie</RegionName>
      </State>
    </Country>
</Continent>
</Continents> 
   ');



SELECT
  unnest(xpath('/Continents/Continent/Country/CountryName/text()', Name)) AS CountryName ,
  unnest(xpath('/Continents/Continent/ContinentName/text()', Name)) AS ContinentName
 into Temp2
FROM XMLTEST1;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...