compiler errors - How should I use an XSLT if statement properly? -
I have created an XSLT stylesheet that looks at a node and removes it. It works great, now I want to check to see if a certain node exists and then remove that node, if it is.
So I tried to add an I-statement and I went to the following error:
compilation error: file dt.xls line 10 element template # Element template is allowed only as a child of the stylesheet
I think I understand the mistake but do not make sure how to go around it.
& lt; Xsl: stylesheet version = "1.0" xmlns: xsl = "http://www.w3.org/1999/ XSL / conversion" & gt; & Lt; Xsl: output omit-xml-declaration = "yes" indent = "yes" /> & Lt; Xsl: template match = "ads" & gt; & Lt; Xsl: template match = "node () | @ *" & gt; & Lt; Xsl: if test = "name-ad-size" & gt; & Lt; XSL: Copy & gt; & Lt; Xsl: apply-select template = "node () | @ *" /> & Lt; / XSL: Copy & gt; & Lt; / XSL: If & gt; & Lt; / XSL: Templates & gt; & Lt; / XSL: Templates & gt; & Lt; Xsl: Template Match = "Fill-Ad-Width" /> & Lt; Xsl: strip-space element = "*" /> & Lt; Xsl: protected-space element = "code sample sample output" /> & Lt; Xsl: template match = "@ * | node ()" & gt; & Lt; XSL: Copy & gt; & Lt; Xsl: apply-select template = "@ * | node ()" /> & Lt; / XSL: Copy & gt; & Lt; / XSL: Templates & gt; & Lt; / XSL: stylesheet & gt;
The problem is, usually, when people try the XSLT they think That it is used to communicate with computers like any other language, such as C #, Java, PHP, all these languages. But with Xslt it is reverse, you tell the processor that depending on the rules you expect the product.
Occasionally, using xsl: if
is good. More often, this is a sign of a mistake. Delete is to create a matching template for nodes, elements, or text that does not output anything like this:
& lt ;! - Start point - & gt; & Lt; Xsl: template match = "/" & gt; & Lt; Xsl: Choose Apply-Template = "Root / Something" /> & Lt; / XSL: Templates & gt; & Lt; Xsl: Template Match = "Name-Ad-Size" & gt; & Lt ;! - Do nothing, process the rest of the document - & gt; & Lt; Xsl: apply-select template = "node () | @ *" /> & Lt; / XSL: Templates & gt; & Lt ;! - Copy anything - & gt; & Lt; Xsl: template match = "node () | @ *" & gt; & Lt; XSL: Copy & gt; & Lt; Xsl: apply-select template = "node () | @ *" /> & Lt; / XSL: Copy & gt; & Lt; / XSL: Templates & gt;
Why does this work? Just because the processor goes through each element and node and looks first on the best matching template. A node & lt; Name-ad-size & gt; The best match for
is the match, which does not output anything, so it effectively removes it. Other nodes do not match, and therefore end in the "catch all" template.
Note 1: The error you received is probably because you accidentally deleted & lt; Xsl: template & gt; / Code> inside another element it is only root
& lt; Xsl: stylesheet & gt;
can be placed anywhere else.
Note 2: EDIT: Someone has magically retrieved your code, the processor will use them to find the best matches, even if they Where to place (as far as they are directly under the root). Here's the story applied to your entire stylesheet: & lt; Xsl: template & gt; statement is irrelevant
& lt; Xsl: stylesheet version = "1.0" xmlns: xsl = "http://www.w3.org/1999/XSL/Transform" & gt; & Lt; Xsl: output omit-xml-declaration = "yes" indent = "yes" /> & Lt; Xsl: strip-space element = "*" /> & Lt; Xsl: protected-space element = "code sample sample output" /> & Lt ;! - Note: It is better to clearly have a starting point - & gt; & Lt; Xsl: template match = "/" & gt; & Lt; Xsl: Choose Apply-Template = "Root / Something" /> & Lt; / XSL: Templates & gt; & Lt ;! - Now I believe that you & lt; Advertising & gt; Element - & gt; & Lt; Xsl: template match = "ads" & gt; & Lt; Xsl: apply-select template = "node () | @ *" /> & Lt; / XSL: Templates & gt; & Lt ;! - Note: here you already have & lt; Phy-ad-width & gt; And everything under it - & gt; & Lt; Xsl: Template Match = "Fill-Ad-Width" /> & Lt ;! - Note: copies of all who do not have any other rules - & gt; & Lt; Xsl: template match = "@ * | node ()" & gt; & Lt; XSL: Copy & gt; & Lt; Xsl: apply-select template = "@ * | node ()" /> & Lt; / XSL: Copy & gt; & Lt; / XSL: Templates & gt; & Lt; / XSL: stylesheet & gt;
Comments
Post a Comment