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:35:07 -0000
Hi again,

I forgot to describe a key constraint to the scenario: I store the results at
#2 in an anonymous document node because I need to preserve the contexts of
the children. For example, additional templates might concatenate sibling
<pre> blocks together. If I declare #2 as as="node()*", this context
information is lost and such context-sensitive templates no longer apply to
the <div> children.


  *   Chris


From: Chris Papademetrious christopher.papademetrious@xxxxxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Sunday, March 19, 2023 7:30 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] differentiating between *the* document node and anonymous
document nodes

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
  xmlns:xs=http://www.w3.org/2001/XMLSchema
  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 document's base-uri() in a variable and check it:

  <!-remember input document's 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="/*", but not for "/" (because "/" cannot have
predicates). Is there a better way to differentiate between the input document
node and anonymous document nodes?

-----
Chris Papademetrious
Tech Writer, Implementation Group
(610) 628-9718 home office
(570) 460-6078 cell

XSL-List info and
archive<https://urldefense.com/v3/__http:/www.mulberrytech.com/xsl/xsl-list__
;!!A4F2R9G_pg!ZO3HeA6EOISx8D4b5h5H4ajBWfM5yYPsdnU4pEzOM3amg0EHn86KNAUd8qEwwea
2vpTBeQK0jhlYtMnkDyd__e3x4MPyZTCjnv1jtSoluj4LnT_oAXuM$>
EasyUnsubscribe<https://urldefense.com/v3/__http:/lists.mulberrytech.com/unsu
b/xsl-list/3380743__;!!A4F2R9G_pg!ZO3HeA6EOISx8D4b5h5H4ajBWfM5yYPsdnU4pEzOM3a
mg0EHn86KNAUd8qEwwea2vpTBeQK0jhlYtMnkDyd__e3x4MPyZTCjnv1jtSoluj4LnR24IhIU$>
(by email<>)

Current Thread