[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

differentiating between *the* document node and anonym

Subject: 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:29:40 -0000
 differentiating between *the* document node and anonym
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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.