[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Specify/determine element's "logical" parent
Hi John,
I think I have written what you need at the bottom, but more comments are inline. john-xsl-list wrote: Thanks again for the response. Memory consumption for XML processing in this What is the size of the source? I tend to doubt that creating the key would be worse than // Keys make xsl-life a great deal easier and faster. I don't mean to gripe and I am sorry to bug the list with this, but I seem to You really don't need extensions. Another way to get around extensions is to use the document function. In you can catch the document() call in (for java) an URIResolver and I believe the .NET way is to use an XmlResolver (or something like that). For example: <xsl:apply-templates select="document('some-dynamic-xml')"/> or (easier to pass this in as a parameter, but for example) <xsl:text>Today's date is: </xsl:text> <xsl:apply-templates select="document('get-date')"/> and in the resolver you would return the XML source to the transformation (don't know .NET, so psuedo-code): public Source resolve(String href, String base) { if (href.equals("some-dynamic-xml")) { return project.buildWhatINeed(); } else if (href.equals("get-date")) { return app.getDate(); } else { return project.getFallback(href); } } I haven't even approached "how to navigate up my logical XSL defintily requires a thought process change. But once you 'get it' you will never want to go back. The manual is the XSL 1.0 specification: http://w3.org/TR/xslt That is probably going to be more difficult to understand than simply getting Mike Kay's book. You don't have to read it cover to cover. It is a reference book with many useful examples. For instance, I do not understand the difference between the two templates That is because you are getting the value of the id attribute in the second one. In the first you are getting a nodeset (in this case just one). I am surpised it gives an error, rather than just print nothing. If your item element looked like: <item id="123">some label</item> then the value-of would give you 'some label' Nor do I understand why when both templates are in a single Well, you have the same match. You can use the mode attribute to do different things with the same matches. Any help or suggestions would be appreciated - hopefully I am doing something really stupid and obvious (at least I got rid of xsl:for-each!). This choose is not necessary as you already have what you are writing out with value-of. You just want: <xsl:choose> <xsl:when test="@parentid"> <xsl:value-of select="@parentid" /> </xsl:when> <xsl:otherwise> <xsl:value-of select="../@id" /> </xsl:otherwise> </xsl:choose> Same thing, right? <xsl:choose> I think you have very difficult (bad) XML to work with, but perhaps you want something like: <xsl:key name="items-with-other-parents" match="item" use="@parentid"/> <xsl:template match="/"> <html> <body> <xsl:apply-templates/> </body> </html> </xsl:template> <> <xsl:template match="item"> <xsl:variable name="id" select="@id"/> <!-- check to see if this item has children not nested directly beneath --> <xsl:variable name="stray-children" select="key('items-with-other-parents', $id<>)"/> <xsl:choose> <!-- does it have children directly nested? --> <xsl:when test="*"> <ul> <li class="container"> <xsl:value-of select="$id"/> </li> <!-- use the $stray-children variable to list all items gathered by the key or //item[@parentid=$id] --> <xsl:apply-templates select="$stray-children"/> <><!-- recursively call this template with the directly nested children --> <xsl:apply-templates/> <> </ul> </xsl:when> <xsl:otherwise> <li> <xsl:value-of select="$id"/> </li> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="item" mode="list-children"> <li> <xsl:value-of select="@id"/> </li> </xsl:template> <> Does this make sense? best, -Rob <?xml-stylesheet type="text/xsl" href="data.xsl"?> <items> <item id="A1"> <item id="A2"> <item id="A3" /> </item> </item> <item id="B1"> <item id="B2"> <item id="B3" parentid="A2" /> </item> </item> </items>
|
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
|