xslt cookbook phần 3

XSLT. Michael Kay cuộc gọi bối cảnh nhạy cảm và cây biến tạm thời. Bối cảnh nhạy cảm biến phát sinh trong lồng nhau cho mỗi vòng khi vòng lặp bên trong phải đề cập đến bối cảnh hiện nay của vòng lặp bên ngoài. 4-12 Ví dụ, đoạn mã được vay mượn từ 4-14 Ví dụ trong công thức , product1 là một bối cảnh nhạy cảm biến cần thiết, | XSLT. Michael Kay calls them context-sensitive and temporary-tree variables. Context-sensitive variables arise in nested for-each loops when the inner loop must refer to the current context of the outer loop. In Example 4-12 the code fragment is borrowed from Example 4-14 in Recipe product1 is a necessary context-sensitive variable while product2 simply helps clarify the code. Example 4-12. A context-sensitive variable xsl template name process-products xsl param name products xsl for-each select products xsl variable name product1 select . xsl for-each select products xsl variable name product2 select . Don t analyze the product against itself -- xsl if test generate-id product1 generate-id product2 xsl call-template name show-products-sold-in-common xsl with-param name product1 select product1 xsl with-param name product2 select product2 xsl call-template xsl if xsl for-each xsl for-each xsl template Temporary-tree variables arise when you want to store the result of XSLT code in a variable. This scenario arises most often when capturing the value returned by the call to a template see Example 4-13 . Example 4-13. Storing a temporary tree xsl varaible name result-tree xsl call-template name compute-something xsl varaible XSLT and for the processors that support it allow such variables to be processed as node sets which is very convenient if you want to do anything significant with them. XSLT calls them result-tree fragments and you cannot do much more than output them without first applying a nonstandard extension node-set function. With 20 20 hindsight result-tree fragments are the classic example of a really bad idea. However when XSLT was developed it was unclear how it would be used and whether technical problems would prevent the creation of temporary node trees. Use xsl key if nodes will be selected by static criteria frequently Of all the optimization methods keys those of xsl key are the most likely to give you the biggest bang