Am 3/19/2023 um 12:29 PM schrieb Chris Papademetrious
christopher.papademetrious@xxxxxxxxxxxx:
>
> Hi everyone,
>
> Given the following input:
>
> <?xml version="1.0" encoding="utf-8" ?>
>
> <html>
>
> B <body>
>
> B B B <div>
>
> <p>line 1</p>
>
> <p>line 2</p>
>
> B B B </div>
>
> B </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">
>
> B B <xsl:mode on-no-match="shallow-copy"/>
>
> B <xsl:output method="xml" indent="yes"/>
>
> B B <!-- add comment to root node of document -->
>
> B <xsl:template match="/*">B <!-- #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>
>
> B <!-- ungroup <div> using a temporary variable for the contents -->
>
> B <xsl:template match="div">
>
> <xsl:variable name="result">B <!-- #2 -->
>
> <xsl:sequence select="node()"/>
>
> </xsl:variable>
>
> <xsl:apply-templates select="$result"/>B <!-- #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-->
>
> B B <body>
>
> <p><!--some info about the document-->line 1</p>
>
> <p><!--some info about the document-->line 2</p>
>
> B B </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:
>
> B <!bremember input documentbs URI -->
>
> B <xsl:variable name="doc-uri" as="xs:string" select="base-uri(.)"/>
>
> B <!-- add comment to root node of document -->
>
> B <xsl:template match="/*[base-uri() = $doc-uri]">B <!-- #1 -->
>
> B B B ...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
B <xsl:variable name="main-doc" select="/"/>
and can then match on
B B document-node()[. is $main-doc]/*
|