一个XSLT/两个XML作为源
第一个XML一个XSLT/两个XML作为源,xml,xslt,Xml,Xslt,第一个XML <?xml version="1.0"?> <response> <status> <code>0</code> </status> <newsList> <news> <id>1</id> <title>some</title> <date>30.11.2011T00:00.00</date> <short
<?xml version="1.0"?>
<response>
<status>
<code>0</code>
</status>
<newsList>
<news>
<id>1</id>
<title>some</title>
<date>30.11.2011T00:00.00</date>
<shortText>some short text</shortText>
<important>LOW</important>
</news>
第二个XML
<?xml version="1.0"?>
<response>
<status>
<code>0</code>
</status>
<newsList>
<news>
<id>1</id>
<text>
Some text here
</text>
</news>
我正在浏览器中转换XSLT
<script>
// the loadXMLDoc function loads the XML and XSL files.
//It checks what kind of browser the user has and loads the file.
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send(null);
xhttp.open
return xhttp.responseXML;
}
//The displayResult() function is used to display the XML file styled by the XSL file.
function displayNewsOverview(xm_l,xs_l)
{
// Load XML and XSL file
xml=loadXMLDoc(xm_l +".xml");
xsl=loadXMLDoc(xs_l +".xsl");
//Test what kind of browser the user has
// If the user has a browser supporting the ActiveX object (IE)
if (window.ActiveXObject)
{
// Use the transformNode() method to apply the XSL style sheet to the xml document
// Set the body of the current document (id="news-overview") to contain the styled xml document
ex=xml.transformNode(xsl);
document.getElementById("news-overview").innerHTML=ex;
}
// If the user has a browser that does not support the ActiveX object
else if (document.implementation && document.implementation.createDocument)
{
// Create a new XSLTProcessor object and import the XSL file to it
xsltProcessor=new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
// Use the transformToFragment() method to apply the XSL style sheet to the xml document
resultDocument = xsltProcessor.transformToFragment(xml,document);
// Set the body of the current document (id="example") to contain the styled xml document
document.getElementById("news-overview").appendChild(resultDocument);
}
}
function displayNewsDetails(xm_l,xs_l)
{
document.getElementById("news-overview").innerHTML="";
// Load XML and XSL file
xml=loadXMLDoc(xm_l +".xml");
xsl=loadXMLDoc(xs_l +".xsl");
//Test what kind of browser the user has
// If the user has a browser supporting the ActiveX object (IE)
if (window.ActiveXObject)
{
// Use the transformNode() method to apply the XSL style sheet to the xml document
// Set the body of the current document (id="news-overview") to contain the styled xml document
ex=xml.transformNode(xsl);
document.getElementById("news-overview").innerHTML=ex;
}
// If the user has a browser that does not support the ActiveX object
else if (document.implementation && document.implementation.createDocument)
{
// Create a new XSLTProcessor object and import the XSL file to it
xsltProcessor=new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
// Use the transformToFragment() method to apply the XSL style sheet to the xml document
resultDocument = xsltProcessor.transformToFragment(xml,document);
// Set the body of the current document (id="example") to contain the styled xml document
document.getElementById("news-overview").appendChild(resultDocument);
}
}
</script>
</head>
onLoad="displayNewsOverview('news-overview', 'news-overview')">
结果应该是第一个XML和第二个XML的标题日期和短文本
任何关于如何在XSLT中实现的线索。我正在考虑使用这个文件
低于我目前得到的XSLT
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<h2>News list</h2>
<table border="1">
<xsl:for-each select="newsList/news">
<tr>
<td><xsl:value-of select="title" /></td>
<td><xsl:value-of select="shortText" /></td>
<td><xsl:value-of select="date" /></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
新闻列表
最后是我用来在浏览器中加载HTML格式的转换XSLT的脚本
<script>
// the loadXMLDoc function loads the XML and XSL files.
//It checks what kind of browser the user has and loads the file.
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send(null);
xhttp.open
return xhttp.responseXML;
}
//The displayResult() function is used to display the XML file styled by the XSL file.
function displayNewsOverview(xm_l,xs_l)
{
// Load XML and XSL file
xml=loadXMLDoc(xm_l +".xml");
xsl=loadXMLDoc(xs_l +".xsl");
//Test what kind of browser the user has
// If the user has a browser supporting the ActiveX object (IE)
if (window.ActiveXObject)
{
// Use the transformNode() method to apply the XSL style sheet to the xml document
// Set the body of the current document (id="news-overview") to contain the styled xml document
ex=xml.transformNode(xsl);
document.getElementById("news-overview").innerHTML=ex;
}
// If the user has a browser that does not support the ActiveX object
else if (document.implementation && document.implementation.createDocument)
{
// Create a new XSLTProcessor object and import the XSL file to it
xsltProcessor=new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
// Use the transformToFragment() method to apply the XSL style sheet to the xml document
resultDocument = xsltProcessor.transformToFragment(xml,document);
// Set the body of the current document (id="example") to contain the styled xml document
document.getElementById("news-overview").appendChild(resultDocument);
}
}
function displayNewsDetails(xm_l,xs_l)
{
document.getElementById("news-overview").innerHTML="";
// Load XML and XSL file
xml=loadXMLDoc(xm_l +".xml");
xsl=loadXMLDoc(xs_l +".xsl");
//Test what kind of browser the user has
// If the user has a browser supporting the ActiveX object (IE)
if (window.ActiveXObject)
{
// Use the transformNode() method to apply the XSL style sheet to the xml document
// Set the body of the current document (id="news-overview") to contain the styled xml document
ex=xml.transformNode(xsl);
document.getElementById("news-overview").innerHTML=ex;
}
// If the user has a browser that does not support the ActiveX object
else if (document.implementation && document.implementation.createDocument)
{
// Create a new XSLTProcessor object and import the XSL file to it
xsltProcessor=new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
// Use the transformToFragment() method to apply the XSL style sheet to the xml document
resultDocument = xsltProcessor.transformToFragment(xml,document);
// Set the body of the current document (id="example") to contain the styled xml document
document.getElementById("news-overview").appendChild(resultDocument);
}
}
</script>
</head>
onLoad="displayNewsOverview('news-overview', 'news-overview')">
//loadXMLDoc函数加载XML和XSL文件。
//它检查用户使用的浏览器类型并加载文件。
函数loadXMLDoc(dname)
{
if(window.XMLHttpRequest)
{
xhttp=newXMLHttpRequest();
}
其他的
{
xhttp=新的ActiveXObject(“Microsoft.XMLHTTP”);
}
xhttp.open(“GET”、dname、false);
xhttp.send(空);
xhttp.open
返回xhttp.responseXML;
}
//函数的作用是:显示由XSL文件设置样式的XML文件。
函数displayNewsOverview(xm_l,xs_l)
{
//加载XML和XSL文件
xml=loadXMLDoc(xm_l+“.xml”);
xsl=loadXMLDoc(xs_l+“.xsl”);
//测试用户使用的浏览器类型
//如果用户的浏览器支持ActiveX对象(IE)
if(window.ActiveXObject)
{
//使用transformNode()方法将XSL样式表应用于xml文档
//将当前文档(id=“news overview”)的主体设置为包含样式化的xml文档
ex=xml.transformNode(xsl);
document.getElementById(“新闻概述”).innerHTML=ex;
}
//如果用户的浏览器不支持ActiveX对象
else if(document.implementation&&document.implementation.createDocument)
{
//创建一个新的XSLTProcessor对象并将XSL文件导入其中
xsltProcessor=新的xsltProcessor();
导入样式表(xsl);
//使用transformToFragment()方法将XSL样式表应用于xml文档
resultDocument=xsltProcessor.transformToFragment(xml,文档);
//将当前文档(id=“example”)的主体设置为包含样式化的xml文档
document.getElementById(“新闻概述”).appendChild(resultDocument);
}
}
函数displayNewsDetails(xm_l,xs_l)
{
document.getElementById(“新闻概述”).innerHTML=“”;
//加载XML和XSL文件
xml=loadXMLDoc(xm_l+“.xml”);
xsl=loadXMLDoc(xs_l+“.xsl”);
//测试用户使用的浏览器类型
//如果用户的浏览器支持ActiveX对象(IE)
if(window.ActiveXObject)
{
//使用transformNode()方法将XSL样式表应用于xml文档
//将当前文档(id=“news overview”)的主体设置为包含样式化的xml文档
ex=xml.transformNode(xsl);
document.getElementById(“新闻概述”).innerHTML=ex;
}
//如果用户的浏览器不支持ActiveX对象
else if(document.implementation&&document.implementation.createDocument)
{
//创建一个新的XSLTProcessor对象并将XSL文件导入其中
xsltProcessor=新的xsltProcessor();
导入样式表(xsl);
//使用transformToFragment()方法将XSL样式表应用于xml文档
resultDocument=xsltProcessor.transformToFragment(xml,文档);
//将当前文档(id=“example”)的主体设置为包含样式化的xml文档
document.getElementById(“新闻概述”).appendChild(resultDocument);
}
}
onLoad=“displayNewsOverview('news-overview','news-overview')”>
任何帮助都会非常感激的。这是为了一份工作
谢谢。您可以在xslt中使用document()函数。您可以尝试将
<?xml样式表href=“nameofxslt”type=“text/xsl”>
添加到一个xml中,并使用document函数访问另一个xml的内容。通过添加xml指令(我是否需要将xslt href添加到xml以使用文档?如果使用javascript代码转换xml,则不需要。如果尝试直接在浏览器中加载xml而不使用任何转换代码,则需要添加“xslt href”。