[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Numbering non-contiguous nodes
Hello all, I'm looking for the simplest and most elegant solution to a node numbering problem. Below is an example of the relevant part of my XML code: =========== XML SNIPPET ***************** <question> The <gap> <option> <value>blue whale</value> <feedback>Are whales really fish?</feedback> </option> <option> <value>giant squid</value> <feedback>Have you ever seen a fish with ten arms?</feedback> </option> <option> <value>whale shark</value> <feedback>Congratulations!</fee dback> </option> <alt_feedback>Make sure you think of a FISH.</alt_feedback> </gap> is the largest fish on Earth. It feeds mainly on <gap> <option> <value>human flesh</value> <feedback>Does the text say that all sharks are aggressive?</feedback> </option> <option> <value>fish</value> <feedback>Are shrimp a kind of fish?</feedback> </option> <option> <value>krill</value> <feedback>All right!</feedback> </option> <alt_feedback>Which other animal are whale sharks compared to?</alt_feedback> </gap> . </question> =========== As you can see, each "question" element may contain text and any number of "gap" elements. Now, basically, I need my output to be like this: =============== DESIRED OUTPUT ********************** <p>The [_1_] is the largest fish on Earth. It feeds mainly on [_2_].</p> =============== In other words, all text nodes should appear in the output, but the "gap" element nodes should be replaced with "[_" + gap number + "_]", where "gap number" is the sequential number of each "gap" element. I thought of using XSLT code along these lines: ============== PROPOSED XSLT ********************* <xsl:template match="question"> <xsl:for-each select="node()"> <xsl:choose> <xsl:when test="local-name() = 'gap'"> <xsl:text>[_</xsl:text> <xsl:value-of select="position()"/> <xsl:text>_]</xsl:text> </xsl:when> <xsl:otherwise> <xsl:value-of select="."/> </xsl:otherwise> </xsl:choose> </xsl:for-each> </xsl:template> ============== The problem is that I only want the "gap" nodes to be counted, but the above solution counts all child nodes of "question" (including the text nodes), so the numbering is incorrect for my purposes. I thought of using a variable as a counter that would be incremented each time <xsl:when test="local-name() = 'gap'"> evaluated to true, but after considerable research have come to the [possibly inaccurate] conclusion that XSL variables cannot be reassigned values this way. Any and all suggestions will be greatly appreciated. Erik XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
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
|