Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ngWpI-0005Dn-UN for pgsql-general@arkaria.postgresql.org; Mon, 18 Apr 2022 19:14:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ngWpH-000243-15 for pgsql-general@arkaria.postgresql.org; Mon, 18 Apr 2022 19:14:19 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ngWpG-00023u-LB for pgsql-general@lists.postgresql.org; Mon, 18 Apr 2022 19:14:18 +0000 Received: from mail-ed1-x531.google.com ([2a00:1450:4864:20::531]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ngWpC-0007RZ-RM for pgsql-general@lists.postgresql.org; Mon, 18 Apr 2022 19:14:17 +0000 Received: by mail-ed1-x531.google.com with SMTP id c64so18491199edf.11 for ; Mon, 18 Apr 2022 12:14:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=dXCvWydryLJj0Hy4qbYXrTmpfabYBCj70UUKbmC3V8U=; b=fXeFoj068kJ0i/PuTQK7U0cVDiPPp0rZ6WAUogzmCMI4iBXIPu+I/ZW/VfjYHzyEBq lBRVeXzWdUT+PgA3PYxxBLFeUWtdQ38nVidsMSHc1A6qGrkqVxV2qStFiMVSgL/8orsU u20ILrGDp1wJTKwsVCn7AnfArhB8CE3TlaC8zlDl6Q54I60aXP1K3+a5DNmvr4pKHN2n FR4m3cFl2qBl08bGKkg3/TIHu9lD/eDpI1Pz+g3KoQQd3IuH+BvvsfNAIZx4+6HA0MBk vVTTB0DyzyAyZancQsiHrPuinJULaEbOa8kxxQwWSSgmQYl14hydfbYMr2bjw//Hx2eE SlYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=dXCvWydryLJj0Hy4qbYXrTmpfabYBCj70UUKbmC3V8U=; b=vcNYQWSwBTa9vw4nP8acR6/wJOlDIf3CFZx9AGqwB6ZOmikfXEDeXfzzIQKYVx1Q4P G4GIm7oKdvMlq0GBbHnObenfQmvFFWBZ1rQrS7gAB3MPvtLNe2R+P4+372/u3M70FVlo CF+Mi6jReWAxzOAjv0+kqOGrVHanl72DGrdOFuvlCMYooAt6IaeNo7End/XJfu+iPdXY L6o8t7wCYevSRVi5clDDkb8+LagB2K29QY8ebbYV5yb+D5XnbnnGbfOFDN4G+NTdEW36 wBXMtKCkUvqH1cpVme+yHXm+g787V+UbsCbQzSLCepV6jKar+t70jp4/bW9B7XTOXFn5 BAuA== X-Gm-Message-State: AOAM533VIei2Jha3C2dCM68QbQcb06mSPQ7gv+UIbUqQG3WJFQYeUWul TBRLV++Uh3uZvckbI8bk1Hl/PbW5eW0= X-Google-Smtp-Source: ABdhPJwtmExkmH2ng0/qu8RUCO0McrvmOHlcNzeRJSP3wEW/8QJy0KxbpXeeqCJKK8v2xD0onzWRzA== X-Received: by 2002:a05:6402:11cd:b0:41d:8dae:3123 with SMTP id j13-20020a05640211cd00b0041d8dae3123mr13375741edw.195.1650309252562; Mon, 18 Apr 2022 12:14:12 -0700 (PDT) Received: from hollewijn.internal (217-19-30-105.dsl.cambrium.nl. [217.19.30.105]) by smtp.gmail.com with ESMTPSA id 27-20020a17090600db00b006df6b34d9b8sm4836731eji.211.2022.04.18.12.14.11 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 18 Apr 2022 12:14:11 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.7\)) Subject: Re: Puzzline CROSS JOIN when doing RECURSIVE CTE From: Alban Hertroys In-Reply-To: Date: Mon, 18 Apr 2022 21:14:11 +0200 Cc: PostgreSQL General Content-Transfer-Encoding: quoted-printable Message-Id: <0E6648DF-32A4-426C-BCE2-C02392CADDD2@gmail.com> References: <75A77395-B71B-438C-A48B-9731A19DDCDF@gmail.com> To: =?utf-8?B?UMOzbCBVYSBMYW/DrW5lY2jDoWlu?= X-Mailer: Apple Mail (2.3608.120.23.2.7) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 18 Apr 2022, at 14:51, P=C3=B3l Ua Lao=C3=ADnech=C3=A1in = wrote: >=20 > Hi Alban, and many thanks for your input. >=20 >> My first question is why you=E2=80=99re using a recursive CTE here? = This doesn=E2=80=99t appear to be hierarchical data (such as a tree), = unless perhaps you intended to actually traverse the HTML document = hierarchy? >=20 > This is basically an exercise on my part. >=20 > The question that I'm trying to answer is here: >=20 > = https://stackoverflow.com/questions/70574881/how-can-get-html-inner-tag-in= -posgresql >=20 > I've already answered it in 3 different ways - but I was trying to do > it with RCTEs in order to improve my comprehension of them. >=20 > So, basically, I want to pick out a subsection of text from a = "passage". >=20 > So then, I wanted to establish a true/false state for the lines that I > want and don't want, going through line by line. I know that the RCTE > is a very contrived way of doing this, but it's for learning really. Considering that you=E2=80=99re already looking at the elements of a = parsed DOM tree, the exercise boils down to traversing that tree. Due to = how xmlparse() is implemented, you probably already get them in the = right order even when not using an explicit order by. That is, if = you=E2=80=99re looking for a DFT (depth first traversal) as opposed to a = BFT (breadth first). One of the difficulties here is that there are some CDATA sections = involved with more XML in them. My guess is that that=E2=80=99s the data = that you=E2=80=99re actually after, but that=E2=80=99s just a matter of = entering the document with the correct path I suppose? > I wonder if you could be so kind as to give me a "skeleton" RCTE for > this - I've been staring at this for hours - and it's not that I'm > lazy or haven't studied RCTEs - I wrote this RCTE >=20 > https://stackoverflow.com/a/71674990/470530 >=20 > recently, so it's not as if I'm completely ignorant of RCTEs - I'm > just stuck in a rut. Any help would be appreciated. You would first need to determine the root node(s). Those are the ones = w/o parents, or you may have some other way of determining those. Next is finding all nodes that have an earlier node as their parent. You could go an extra step here with preserving the order of the = siblings in the document, by numbering nodes (directly) under the same = parent. I usually build an ltree structure with that information, while = traversing the tree - that gets you an ltree with entries (1, 1.1, = 1.1.1, 1.1.2, 1.2.1, etc) that you then can use for the final order by, = for example. In case you didn=E2=80=99t know, ltree is a module you can install. I = find it still very useful in tree traversals. The one drawback I see is = that for these scenario=E2=80=99s you=E2=80=99d ideally want an ltree = based on integers, such that 10 sorts after 9 instead of between 1 and = 2. Padding enough zeroes before the ltree text items is a bit of an = extra hassle that I=E2=80=99d prefer to do without. I haven=E2=80=99t actually looked at what DOM navigation functions exist = for PG, so this is more or less pseudo code. Worse, my local copy of PG = was compiled w/o XML support, so I don=E2=80=99t know what kind of = result the query from that SO article produces. But then again, I = don=E2=80=99t really know what you=E2=80=99re after anyway, so... This is basically how I would go about it. with recursive -- First we need to get the DOM-tree parsed (this is not actually = recursive) domtree as ( select node from xmlparse(document(=E2=80=98...')) ), -- Next we can traverse it cte (node, hierarchy, n) as ( select node, 1::text::ltree, 1 from domtree where parent(node) is null union all select node, cte.hierarchy || (cte.n+1)::text::ltree, n+1 from domtree t join cte on parent(t.node) =3D cte.node ) select * from cte order by hierarchy; Function parent() is made-up. It would return the parent node of a node, = so that there is some way to connect the different parts in the = hierarchy. I guess xpath() could fulfil that purpose, but I have no way = of testing that hypothesis. I hope that=E2=80=99s a good enough starting point for you? Alban Hertroys -- There is always an exception to always.