Re: [xsl] Match Parents Element ID

Subject: Re: [xsl] Match Parents Element ID
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 27 Jan 2025 09:30:16 -0000
On 27/01/2025 09:58, Byomokesh Sahoo sahoo.byomokesh@xxxxxxxxx wrote:
Hi,

We have multiple XML files which need to fetch parent elements with
attributes only from one file. Find below my xsl and input files.

ID File: <!-- Take chapter based on the section file -->
<gd id="RB_A">
B  B  B  B  <act id="RB_C">
B  B  B  B  B  B  <title>SET</title>
B  B  B  B  B  B  <body>
B  B  B  B  B  B  B  B  <chap id="RB_CHAP1">
B  B  B  B  B  B  B  B  B  B  <sect id="RB_S1"/>
B  B  B  B  B  B  B  B  B  B  <sect id="RB_S2"/>
B  B  B  B  B  B  B  B  </chap>
B  B  B  B  B  B  B  B  <chap id="RB_CHAP2">
B  B  B  B  B  B  B  B  B  B  <sect id="RB_S3"/>
B  B  B  B  B  B  B  B  B  B  <sect id="RB_S4"/>
B  B  B  B  B  B  B  B  </chap>
B  B  B  B  B  B  </body>
B  B  B  B  </act>
B  B  </gd>
Sect file: <!-- Multiple Section file like this -->
B <sce>
B  B  B  B  <gd id="RB_A">
B  B  B  B  B  B  <act id="RB_C">
B  B  B  B  B  B  B  B  <title>TITLE</title>
B  B  B  B  B  B  B  B  <body>
B  B  B  B  B  B  B  B  B  B  <sect id="RB_S1">
B  B  B  B  B  B  B  B  B  B  <title>Tile</title>
B  B  B  B  B  B  B  B  B  B  <p>Paragrph</p>
B  B  B  B  B  B  B  B  B  B  </sect>
B  B  B  B  B  B  B  B  </body>
B  B  B  B  B  B  </act>
B  B  B  B  </gd>
B  B  </sce>
Desired File:
B <sce>
B  B  B  B  <gd id="RB_A">
B  B  B  B  B  B  <act id="RB_C">
B  B  B  B  B  B  B  B  <title>TITLE</title>
B  B  B  B  B  B  B  B  <body>
B  B  B  B  B  B  B  B  B  B  <chap id="RB_CHAP1">
B  B  B  B  B  B  B  B  B  B  <sect id="RB_S1">
B  B  B  B  B  B  B  B  B  B  <title>Title</title>
B  B  B  B  B  B  B  B  B  B  <p>Paragraph</p>
B  B  B  B  B  B  B  B  B  B  </sect>
B  B  B  B  B  B  B  B  B  B  </chap>
B  B  B  B  B  B  B  B  </body>
B  B  B  B  B  B  </act>
B  B  B  B  </gd>
B  B  </sce>

XSL:


<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";> B B <xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes" /> B B <xsl:variable name="ch" select="'ID.xml'" /> B B <xsl:template match="@*|node()"> B B B B <xsl:copy> B B B B B B <xsl:apply-templates select="@*|node()" /> B B B B </xsl:copy> B B </xsl:template>

B  B  <xsl:template match="sce/gd/act/body">
B  B  B  B  <xsl:variable name="info"
select="document($ch)//sce/gd/act/body/chap/sect[@id=current()/@id]/." />
B  B  B  B  <chap id="">
B  B  B  B  B  B  <xsl:attribute name="id">
B  B  B  B  B  B  B  B  <xsl:value-of select="$info[parent::chap[@id]]"/>
B  B  B  B  B  B  </xsl:attribute>
B  B  B  B  B  B  <xsl:copy>
B  B  B  B  B  B  B  B  <xsl:apply-templates select="@*|node()" />
B  B  B  B  B  B  B  B  <xsl:for-each select="$info/*">
B  B  B  B  B  B  B  B  B  B  B  B  <xsl:copy-of select="." />
B  B  B  B  B  B  B  B  </xsl:for-each>
B  B  B  B  B  B  </xsl:copy>
B  B  B  B  </chap>
B  B  </xsl:template>


I think you want

B B  <xsl:template match="sce/gd/act/body">
B B B B B B B  <xsl:variable name="info"
select="document($ch)//gd/act/body/chap[sect[@id=current()/sect/@id]]"/>
B B B B B B B  <chap>
B B B B B B B B B B B  <xsl:attribute name="id">
B B B B B B B B B B B B B B B  <xsl:value-of select="$info/@id"/>
B B B B B B B B B B B  </xsl:attribute>
B B B B B B B B B B B  <xsl:copy>
B B B B B B B B B B B B B B B  <xsl:apply-templates select="@*|node()" />
B B B B B B B B B B B B B B B  <xsl:for-each select="$info/*">
B B B B B B B B B B B B B B B B B B B B B B B  <xsl:copy-of select="." />
B B B B B B B B B B B B B B B  </xsl:for-each>
B B B B B B B B B B B  </xsl:copy>
B B B B B B B  </chap>
B B B  </xsl:template>

There is no "sce" in the ID file so the path

B document($ch)//sce/gd/act/body/chap

seems wrong.

Current Thread