Subject: [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:29:40 -0000 |
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] How to remove outer tag i, Peter Flynn peter@xx | Thread | Re: [xsl] differentiating between *, Chris Papademetrious |
Re: [xsl] How to remove outer tag i, Matt Van Voorhies mv | Date | Re: [xsl] differentiating between *, Chris Papademetrious |
Month |