Why xslt is used in xml
If you do not delete the namespace declaration that is embedded in the Project element, XPath statements will fail to match elements in the Project XML file. After you perform Steps 2 and 3, the first three lines of your Project XML file should look like the following code example. View the transformed Project XML file.
To see the transformed output you must select the ProjectTransform. What is XSLT? XSLT Reference. XPath Reference. Coat meat with the bread crumbs and parmesan cheese. Heat olive oil in a large frying pan, and saute 1 teaspoon of the garlic for 3 minutes. Quick fry brown quickly on both sides meat. Place meat in a casserole baking dish, slightly overlapping edges.
Place onion rings and peppers on top of meat, and pour marinara sauce over all. Sprinkle mozzarella over meat and leave in the oven till bubbly. Drain, and toss in butter and 1 teaspoon garlic. For a stronger garlic taste, season with garlic powder. Top with grated parmesan and parsley for color.
Serve meat and sauce atop a mound of pasta! If you do this, save the mozzarella till the last minute. Stir in the ricotta cheese and the chocolate chips. Set aside. Cut in the shortening and mix until the mixture resembles coarse crumbs.
Mix in 4 of the eggs and 1 teaspoon of the vanilla. Divide dough into 4 balls and chill if needed. Grease two deep dish pie plates. Do not make the crust too thick as it will expand during cooking and get too thick. Do not flute the edges of the dough. Roll out the other 2 balls of dough and cut each into 8 narrow strips for the top of the crust. Alternately you can use cookie cutters and place the cutouts on the top of the pies. Top each pie with 8 narrow strips of dough or cookie cut-outs.
Brush top of pie with milk for shine. Place foil on the edge of crust. Continue to bake for another 25 or 30 minutes or until a knife inserted in the center comes out clean. February 4, For the first stage, you select text nodes, as well as inline tags such as bold and italics, using the XPath text function.
The vertical pipe is used to match multiple items: text or a bold tag or an italics tag, etc. In the second stage, you select the other structure elements contained in the file, for sections, paragraphs, lists, and notes. In general, using the select clause lets you apply all templates to a subset of the information available in the current context.
As another example, this template selects all attributes of the current node:. Next, add the virtually identical template to process subheadings that are nested one level deeper:.
You could add templates for deeper headings, too, but at some point you must stop, if only because HTML goes down only to five levels. For this example, you will stop at two levels of section headings. But if the XML input happens to contain a third level, you will want to deliver an error message to the user. This section shows you how to do that. But instead we will take the opportunity to play with messaging.
Add the following template to generate an error when a section is encountered that is nested too deep:. Without it, processing could still go on, with everything in that section being ignored. Generate an error on any section nested five levels deep. Note - The code discussed in this section is in Stylizer. The result is stylizer1a.
The main differences between the two programs are described below. This code uses the file to create a StreamSource object and then passes the source object to the factory class to get the transformer. At this point, there is quite a bit of excess whitespace in the output. In the next section, you will see how to eliminate most of it. Recall that when you look at the structure of a DOM, there are many text nodes that contain nothing but ignorable whitespace.
Most of the excess whitespace in the output comes from these nodes. Fortunately, XSL gives you a way to eliminate them. Note - The stylesheet discussed in this section is in article1b.
The result is stylizer1b. Nodes that contain text other than whitespace will not be affected, nor will other kinds of nodes. That is quite an improvement. There are still newline characters and whitespace after the headings, but those come from the way the XML is written:.
Here, you can see that the section heading ends with a newline and indentation space, before the PARA entry starts. That is not a big worry, because the browsers that will process the HTML compress and ignore the excess space routinely. But there is still one more formatting tool at our disposal.
Note - The stylesheet discussed in this section is in article1c. The result is stylizer1c. That is quite a bit better. Of course, it would be nicer if it were indented, but that turns out to be somewhat harder than expected. Here are some possible avenues of attack, along with the difficulties:. Even if that option were available, it would not help, because HTML elements are rarely nested! Although HTML source is frequently indented to show the implied structure, the HTML tags themselves are not nested in a way that creates a real structure.
So it could conceivably be used to output indentation space. The problem is to vary the amount of indentation space. XSLT variables seem like a good idea, but they do not work here. The reason is that when you assign a value to a variable in a template, the value is known only within that template statically, at compile time. Even if the variable is defined globally, the assigned value is not stored in a way that lets it be dynamically known by other templates at runtime.
Using a parameterized template is another way to modify a template's behavior. But determining the amount of indentation space to pass as the parameter remains the crux of the problem.
At the moment, then, there does not appear to be any good way to control the indentation of HTML formatted output. That would be inconvenient if you needed to display or edit the HTML as plain text. When you view stylizer1c. Note - The sample document described in this section is article2.
The result is stylizer2. Note - Although the list and note in the XML file are contained in their respective paragraphs, it really makes no difference whether they are contained or not; the generated HTML will be the same either way. But having them contained will make them easier to deal with in an outline-oriented editor.
Next, modify the PARA template to account for the fact that we are now allowing some of the structure elements to be embedded with a paragraph:. This modification uses the same technique you used for section headings.
The only difference is that SECT elements are not expected within a paragraph. However, a paragraph could easily exist inside another paragraph-for example, as quoted material. In this case, the value of the type attribute is tested, and the list that is generated changes depending on whether the value is ordered or unordered. Single quotes are required around the attribute values. By now, you should have the idea that templates are independent of one another, so it does not generally matter where they occur in a file.
So from this point on, we will show only the template you need to add.
0コメント