xsl参数:<xsl:param name=""></xsl:param> 等等
xml文件:
<?xml version="1.0" encoding="UTF-8"?><messages> <message name="msg23">Error 23: The drive is full.</message> <message name="msg42">Error 42: The file is not found.</message></messages>
<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><!-- these 2 elements effectively assign $messages = resources/en.xml/<messages>, then $messages is used in the "localized-message" template --><xsl:param name="lang">message</xsl:param><xsl:variable name="messages" select="document(concat('../xmlfiles/', $lang, '.xml'))/messages"/> <xsl:template name="msg23" match="msg23"> <xsl:call-template name="localized-message"> <xsl:with-param name="msgcode">msg23</xsl:with-param> </xsl:call-template></xsl:template><xsl:template name="localized-message"> <xsl:param name="msgcode"/> <!-- show message string --> <xsl:message terminate="yes"> <xsl:value-of select="$messages/message[@name=$msgcode]"/> </xsl:message></xsl:template></xsl:stylesheet>
<xsl:param name="lang">message</xsl:param>这一行是声明了参数名,并给定值为:"message"字符串.
<xsl:variable name="messages" select="document(concat('../xmlfiles/', $lang, '.xml'))/messages"/>这一行也是定义了参数名,与上一行不同的是它用了上一行的值:"$lang",最终得到的值为:"document(../xmlfiles/message.xml)/messages".这两个变量为全局变量。
<!-- a--><xsl:template name="msg23" match="msg23"> <xsl:call-template name="localized-message"> <xsl:with-param name="msgcode">msg23</xsl:with-param> </xsl:call-template></xsl:template><!-- b --><xsl:template name="localized-message"> <xsl:param name="msgcode"/> <!-- show message string --> <xsl:message terminate="yes"> <xsl:value-of select="$messages/message[@name=$msgcode]"/> </xsl:message></xsl:template>
<xsl:with-param name="msgcode">msg23</xsl:with-param>;
<html><head><script src="../js/commUtil.js"></script><script>function parseXml() {var xslDoc = commLoad("../xsl_files/message.xsl");var xmlDoc = commLoad("../xmlfiles/message.xml");var showStr = xmlDoc.documentElement.transformNode(xslDoc);var divContent = document.getElementById("divContent");divContent.innerHTML = showStr;}</script></head><body><div id="divContent"></div><input type="button" onclick="parseXml()" /></body></html>
/** * @param url <xml>文档所在位置 * @return retDom xml的dom * */function commLoad(URL){var retDom = null;try{retDom = new ActiveXObject('Microsoft.XMLDOM');retDom.async = false;retDom.load(URL);}catch(e){retDom = document.implementation.createDocument("", "", null); retDom.async = false;retDom.load(URL);}return retDom;}/** * @param xml <xml>文档 * @return retDom xml的dom * */function commLoadXML(xml){var retDom = null;try{retDom = new ActiveXObject('Microsoft.XMLDOM');retDom.async = false;retDom.loadXML(xml);}catch(e){var oParser = new DOMParser();retDom = oParser.parseFromString(xml,"text/xml");}return retDom;}