开发者

What's the advantage of having a website with two XML tags?

开发者 https://www.devze.com 2022-12-10 22:50 出处:网络
I was over at the StarCraft2 website and decided to take a look at their source and saw this: <?xml version=\"1.0\" encoding=\"UTF-8\"?>

I was over at the StarCraft2 website and decided to take a look at their source and saw this:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="layout/artwork.xsl"?>

<page lang="en_us">
  <artwork/>
</page>
开发者_如何学C

... and that's it. So I was wondering, what's the point in using an XSLT if you're just using 2 tags? Is it to obfuscate the source?

Edit: I just want to clarify that I'm not asking how XSLTs work. I asking what is the advantage of setting up a page in this manner. It seems to me like a poor use of XSLTs.


[EDIT] Providing the complete info in the source xml vs providing it in the stylesheet would be a subjective matter which would depend on the designer of the system. We could assume that in this case, that tiny xml is all that indicates the information pertaining to this page with the rest of the scaffolding coming in the imports/includes. OR that two different teams were working on page layout content and actual page hierarchies letting the layout designers alter content without changes to the xmls, but your guess is as good as mine. Usually this would be designed based on if that XML will be used anywhere else or if the layout information is needed wherever the XML can possibly be reused. If the layout information is not needed anywhere else, drop it and include it in your XSLT leaving behind a cleaner XML that can be reused in multiple places.

This is working as intended. What is happening is that the site is performing an XSLT transformation on the XML file which is via the processing instruction that is in the second line.

 <?xml-stylesheet type="text/xsl"
href="layout/artwork.xsl"?>

Browsers which support XSLT rendering in the client side would fetch the XSLT file specified in the processing instruction and perform an XSLT transformation and display the output. Since this is at runtime, the actual resultant markup will not be displayed when you "View Source", but the original source will only be displayed.

If you load the layout/artwork.xsl file, you would find the code that is needed to render the page in your browser available there. http://starcraft2.com/layout/artwork.xsl Also, the artwork.xsl again imports another stylesheet called includes.xsl - http://starcraft2.com/layout/includes.xsl -which has further code that generates content that will be displayed. Knowledge or awareness of XSLT would help in identifying what the output will be for these stylesheets and how it works.

From what I see, the stylesheets seem to import external XML documents as well for use as input data based on which the output is generated. This is in addition to what is provided as a "Source" document to the XSLT. In this case, the source is very tiny and the bulk of the information is imported in the XSLTs.

Hope I made sense.

Thanks, Thiyag


The way that they are using it is not necessarily bad/wrong, just different than what you are used to seeing.

In this case there is not much XML content to transform, but the structure of the document and the little bit of XML that is present does affect what is produced by the XSLT.

  • The <page> element in the source XML file matches the XSLT template with the boilerplate HTML content
  • The @lang XSLT template match dictates which language specific content to render.
  • The presence of the <artwork/> element in the source XML file is used to trigger the "artwork" template in the XSLT which includes "artwork" specific HTML content(which includes "artwork" specific JavaScript). That content could have been included in the XML file and the template could have copied into the result doc as well.

They have just decided to put all of the content in the XSLT(and JavaScript). It really doesn't matter where that content "lives", both the source XML and the XSLT are XML documents and are used to generate the result document.

There are also lots of JavaScript files included in the resulting HTML page and some came from that artwork template. I didn't take the time to see how it all is working, but I suspect that a lot of the HTML content of the page is also generated from those JavaScript files.

You could ask a similar question about why someone would have chosen to put the HTML content of the HTML page in the JavaScript rather than in the source HTML file that includes the JavaScript with the elements.


Well one thing to notice is that each page element has a lang attribute that is modified based on the Accept-Language http header provided by the browser. The XSLT uses this attribute to select localized resources. The template is also organized in such a way that many common page elements, like headers and flash objects, are defined in a common "includes.xsl" file.

Since all of the localization and html rendering is done by the client, this probably saves a fair amount of server CPU time. Also, given that many elements are defined in a global "includes" file, this can be requested once by the browser and cached, saving bandwidth and reducing the overall time to download each page.


The particular page for which you got the source may have been generated by a content management system.

Some content management systems publish all content as XML then HTML is generated by style sheets applied by the web server.

It is much less common that the web browser is responsible apply the style sheets though that seems to be what is happening in this case.

0

精彩评论

暂无评论...
验证码 换一张
取 消