[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: an xsl transformation is missing an element from i
Hi Tony,
At 02:32 PM 5/27/2008, you wrote: <?xml version="1.0" encoding="UTF-8"?> <test> <div1> <ptr target="a" n="2"/> <ptr target="b" n="15"/> </div1> <div1> <ptr target="c" n="72"/> <ptr target="d" n="3822"/> <ptr target="e" n="3823"/> </div1> </test> They aren't showing up because they are being matched by a template that says to process their ptr element children, but doesn't say to do anything with the div1 elements themselves. You could effect this by adding an xsl:copy instruction to your template: <xsl:template match="div1"> <xsl:copy> <xsl:copy-of select="@*"/> <xsl:for-each select="ptr"> <xsl:element name="ptr"> <xsl:attribute name="target" select="@target"/> <xsl:attribute name="n" select="position()"/> </xsl:element> </xsl:for-each> </xsl:copy> </xsl:template> But an even cleaner solution would match not on the div1 but on the ptr elements, since those are the elements you wish to modify: <xsl:template match="ptr"> <ptr target="{@target}" n="{count(.|preceding-sibling::ptr)}"/> </xsl:template> Notice: * Instead of using the xsl:attribute instruction, attributes are created in a literal result element using the attribute value template syntax (curly braces) to provide the values. This is more succinct. * To generate the value for @n, instead of using the position of the current node in the current node list (which works when you nest in the for-each, but which depends on your traversal logic and so is prone to break when things get more complex), this simply counts the ptr plus its preceding sibling ptr elements, assigning @n by the count. * The div1 elements will still be copied if you leave the generic "identity template" in place (which says to copy it if it isn't matched by another template, which it now isn't). Congratulations on your first stylesheet. You're getting the idea. Cheers, Wendell ====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================
|
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
|