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

Subject: Re: [xsl] differentiating between *the* document node and anonymous document nodes
From: "Michael Kay michaelkay90@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 19 Mar 2023 12:37:08 -0000
>  (because b/b cannot have predicates)

In an XPath expression you can write `(/)[predicate]` or `/.[predicate]`

In an XSLT pattern you can write `/self::node()[predicate]`or
`self::document-node()[predicate]`

Michael Kay
Saxonica

> On 19 Mar 2023, at 11:29, Chris Papademetrious
christopher.papademetrious@xxxxxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> 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
<http://www.w3.org/1999/XSL/Transform>
>   xmlns:xs=http://www.w3.org/2001/XMLSchema
<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 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?
>
> -----
> Chris Papademetrious
> Tech Writer, Implementation Group
> (610) 628-9718 home office
> (570) 460-6078 cell
>
> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/3500899> (by
email <>)

Current Thread