[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: (Possible) pitfall: XSLT 2, 9.4 Creating implicit
At 2012-05-09 17:02 +0200, Christian Roth wrote:
this is a heads-up for a pitfall (that at least I have fallen into several times now...) with respect to XSLT 2, 9.4 "Creating implicit document nodes": Fine. But the declaration you use is so very important. You've declared $v1 to be a tree and $v2 to be an element. So you've declared two very different animals and you cannot use the same syntax to address them. $v1/a and $v2/a are both addressing the <a> child of the variable. Only $v1 has an <a> child variable. And worrisomely if the <a> in $v2 had an <a> child you would be addressing the child rather than what you want. Is there a technique or pattern I could employ (maybe utilizing the @as attribute somehow?) to unify the access to variable contents where I know that the sequences are node sequences, regardless of their content construction using @select or <xsl:sequence>? If you had: <xsl:variable name="v1" as="element()"> <xsl:sequence select="a"/> </xsl:variable> <xsl:variable name="v2" select="a"/> ... then in both $v1 and $v2 *are* <a> (i.e. you wouldn't need to say $v/a to get at <a>). See the working example below. Does that help? It isn't a document tree, but of course <a> can have descendants. If you want multiple elements in a sequence then use element()+. . . . . . . . . . . . Ken T:\ftemp>type christian.xml <?xml version="1.0" encoding="UTF-8"?> <a> <b/> </a> T:\ftemp>xslt2 christian.xml christian.xsl <?xml version="1.0" encoding="UTF-8"?>v1: ab v2: ab T:\ftemp>type christian.xsl <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"> <xsl:template match="/"> <xsl:variable name="v1" as="element()"> <xsl:sequence select="a"/> </xsl:variable> <xsl:variable name="v2" select="a"/>
<xsl:text>v2: </xsl:text> <xsl:apply-templates select="$v2" mode="out"/> </xsl:template>
-- Public XSLT, XSL-FO, UBL and code list classes in Europe -- Oct 2012 Contact us for world-wide XML consulting and instructor-led training Free 5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Google+ profile: https://plus.google.com/116832879756988317389/about Legal business disclaimers: http://www.CraneSoftwrights.com/legal
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|