Re: [xsl] differentiating between *the* document node and anonymous document nodes

Subject: Re: [xsl] differentiating between *the* document node and anonymous document nodes
From: "Chris Papademetrious christopher.papademetrious@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 19 Mar 2023 11:44:34 -0000
Hi Martin,

The $main-doc solution worked perfectly for my needs!

This list is a gem. (You too, Martin.)


  *   Chris


From: Martin Honnen martin.honnen@xxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Sunday, March 19, 2023 7:39 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] differentiating between *the* document node and anonymous
document nodes



Am 3/19/2023 um 12:29 PM schrieb Chris Papademetrious
christopher.papademetrious@xxxxxxxxxxxx<mailto:christopher.papademetrious@syn
opsys.com>:
Hi everyone,

Given the following input:

<?xml version="1.0" encoding="utf-8" ?>
<html>
  <body>
    <div>
      <p>line 1</p>
      <p>line 2</p>
    </div>
  </body>
</html>

I apply the following stylesheet that (1) adds a comment to the root node, and
(2) ungroups <div> elements:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl=http://www.w3.org/1999/XSL/Transform<https://urldefense.com/v3/__ht
tp:/www.w3.org/1999/XSL/Transform__;!!A4F2R9G_pg!e1_KApmfI02y0j5SgYccRvq2b6Po
aUdP0MtX0A4BmIuVI6jiiJeog9P8BX5K8NXC-lqWUS7ayZFa0FoqdCmZevBtoVEwUB9QfYkUbMnq2
cbAXynIDI2_$>
  xmlns:xs=http://www.w3.org/2001/XMLSchema<https://urldefense.com/v3/__http:
/www.w3.org/2001/XMLSchema__;!!A4F2R9G_pg!e1_KApmfI02y0j5SgYccRvq2b6PoaUdP0Mt
X0A4BmIuVI6jiiJeog9P8BX5K8NXC-lqWUS7ayZFa0FoqdCmZevBtoVEwUB9QfYkUbMnq2cbAX0-k
R8oO$>
  version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>
  <xsl:output method="xml" indent="yes"/>

  <!-- add comment to root node of document -->
  <xsl:template match="/*">  <!-- #1 -->
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:comment>some info about the document</xsl:comment>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- ungroup <div> using a temporary variable for the contents -->
  <xsl:template match="div">
    <xsl:variable name="result">  <!-- #2 -->
      <xsl:sequence select="node()"/>
    </xsl:variable>
    <xsl:apply-templates select="$result"/>  <!-- #3 -->
  </xsl:template>

</xsl:stylesheet>

The problem is that when I ungroup <div> into an anonymous document node at
#2, then apply templates to the results at #3, the root node template at #1 is
applied again:

<?xml version="1.0" encoding="UTF-8"?>
<html><!--some info about the document-->
   <body>
      <p><!--some info about the document-->line 1</p>
      <p><!--some info about the document-->line 2</p>
   </body>
</html>

How can I apply a template to the true top-level document node, but not to
anonymous document nodes?

I could store the documentbs base-uri() in a variable and check it:

  <!bremember input documentbs URI -->
  <xsl:variable name="doc-uri" as="xs:string" select="base-uri(.)"/>

  <!-- add comment to root node of document -->
  <xsl:template match="/*[base-uri() = $doc-uri]">  <!-- #1 -->
    ...omitted...
  </xsl:template>

This works for match=b/*b, but not for b/b (because b/b cannot
have predicates). Is there a better way to differentiate between the input
document node and anonymous document nodes?


To me it sounds as if you want to process $result in a different mode.



Or you need e.g. a global

  <xsl:variable name="main-doc" select="/"/>



and can then match on

   document-node()[. is $main-doc]/*


XSL-List info and
archive<https://urldefense.com/v3/__http:/www.mulberrytech.com/xsl/xsl-list__
;!!A4F2R9G_pg!e1_KApmfI02y0j5SgYccRvq2b6PoaUdP0MtX0A4BmIuVI6jiiJeog9P8BX5K8NX
C-lqWUS7ayZFa0FoqdCmZevBtoVEwUB9QfYkUbMnq2cbAXzKr_Ivh$>
EasyUnsubscribe<https://urldefense.com/v3/__http:/lists.mulberrytech.com/unsu
b/xsl-list/3380743__;!!A4F2R9G_pg!e1_KApmfI02y0j5SgYccRvq2b6PoaUdP0MtX0A4BmIu
VI6jiiJeog9P8BX5K8NXC-lqWUS7ayZFa0FoqdCmZevBtoVEwUB9QfYkUbMnq2cbAX7OvqQeM$>
(by email<>)

Current Thread