我正在尝试为朋友创建一个网站,我的经验绝对不是XML / XSLT。
我有一个非常简单的存储过程,它生成XML:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[setXMLData]
as
SELECT
(SELECT title as '@Title',
linkpath as '@Linkpath',
displayOrder as '@displayOrder' FROM dbo.tblSiteMenuSections order by displayorder asc FOR XML PATH('SiteMenu'), TYPE) AS 'MENU',
(SELECT imgPath as '@imgPath',
displayOrder as '@displayOrder' FROM dbo.tblBannerSlides order by displayorder asc FOR XML PATH('SlideShow'), TYPE) AS 'SLIDES'
FOR XML PATH('Site')
返回此XML:
<Site>
<MENU>
<SiteMenu Title="Edo" Linkpath="/default.aspx" displayOrder="0" />
<SiteMenu Title="Infomation" Linkpath="/information.aspx" displayOrder="1" />
<SiteMenu Title="News" Linkpath="/news.aspx" displayOrder="2" />
<SiteMenu Title="Menu" Linkpath="/menu.aspx" displayOrder="3" />
<SiteMenu Title="Gallery" Linkpath="/gallery.aspx" displayOrder="4" />
<SiteMenu Title="Contact" Linkpath="/contact.aspx" displayOrder="5" />
<SiteMenu Title="Catering" Linkpath="/catering.aspx" displayOrder="6" />
</MENU>
<SLIDES>
<SlideShow imgPath="/20160806_182949.jpg" displayOrder="1" />
<SlideShow imgPath="/20160901_193700.jpg" displayOrder="2" />
<SlideShow imgPath="/14202562_319650221718312_9092982335636542049_n.jpg" displayOrder="3" />
<SlideShow imgPath="/14237745_319766348373366_7639011255502415884_n.jpg" displayOrder="4" />
</SLIDES>
</Site>
我正在尝试将此XML加载到chrome中并设置一些基本HTML样式,但它只加载一个空白页面。我正在使用以下代码执行此操作,该代码在页面加载时运行。
private void buildXML()
{
con = new SqlConnection("server=[REDACTED]; database= [TOTALLY NOT GOVERNMENT SECRETS; persist security info=True; Integrated Security=SSPI;");
cmd = new SqlCommand("dbo.setXMLData", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
string xmlPrefix = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><?xml-stylesheet type=\"text/xsl\" href=\"/XSLT/Templates.xslt\"?>";
xRead = cmd.ExecuteXmlReader();
xRead.Read();
xmlPrefix += Environment.NewLine + xRead.ReadOuterXml();
pXml.LoadXml(xmlPrefix);
Response.Write(pXml.OuterXml);
}
然而,当我尝试加载页面时,chrome只显示一个空白页面(xml节点全部嵌套在元素检查器中的其他内容......就像那些小俄罗斯娃娃),IE显示“只有一个根元素”错误。
我觉得我错过了一些明显的东西,但是几个小时的拖网搜索其他问题和谷歌搜索都没有帮助。
修改
这是我正在使用的xslt
样式表,它还有一些额外的内容尚未由SQL提供。
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" indent="yes" encoding="utf-8" media-type="text/html" omit-xml-declaration="yes" doctype-system="about:legacy-compat" />
<xsl:template match="/">
<html>
<head runat="server">
<title>[Site Title]</title>
<link rel="stylesheet" runat="server" href="~/Assets/styles.css" />
<script src="/Assets/jquery-3.1.0.min.js"></script>
<script type="text/javascript" src="/Assets/scripts.js"></script>
<link rel="icon" href="favicon.ico" type="image/x-icon"/>
<link href="https://fonts.googleapis.com/css?family=Lora:700" rel="stylesheet"/>
</head>
<body>
<form id="form1" class="main-page" runat="server">
<xsl:call-template name="main-slideshow"/>
<div id="logo">
<img class="logo_main" src="Images/main_logo.png" />
</div>
<div id="suppliers">
<div class="supplier-images">
<img class="supplier" src="Images/1398770711.png" />
<label class="supplier-label">[name]</label>
</div>
</div>
<xsl:call-template name="menu-setup"/>
<div>
<button id="btnBooking" class="button-booking">Reservation</button>
</div>
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">x</span>
<p>Some text in the Modal..</p>
</div>
</div>
</div>
</form>
</body>
</html>
</xsl:template>
<xsl:template name="main-slideshow">
<div>
<xsl:variable name="slideCount" select="count(//Slideshow)" />
<div id="CSS3Slideshow {$slideCount}">
<xsl:for-each select="//ROOT/SLIDES/Slideshow">
<xsl:variable name="pos" select="position()"/>
<img class="img{$pos}" src="@imgPath"/>
</xsl:for-each>
</div>
<!--<div id="CSS3Slideshow">
<img class="img" src="Images/20160806_182949.jpg" />
<img class="img2" src="Images/20160901_193700.jpg" />
<img class="img3" src="Images/14202562_319650221718312_9092982335636542049_n.jpg" />
<img class="img4" src="Images/14237745_319766348373366_7639011255502415884_n.jpg"/>
</div>-->
</div>
</xsl:template>
<xsl:template name="menu-setup">
<div id="menu-bar">
<ol class="menu-list">
<xsl:for-each select="//ROOT/MENU/SiteMenu">
<xsl:variable name="linkPath" select="@LinkPath"/>
<xsl:choose>
<xsl:when test="position()=1">
<a href="{$linkPath}">
<xsl:value-of select="@Title"/>
</a>
<li class="menu-item-separator-Line">|</li>
</xsl:when>
<xsl:otherwise>
<li class="menu-item-separator-icon">▶</li>
<li class="menu-item">
<a href="{$linkPath}">
<xsl:value-of select="@Title"/>
</a>
</li>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</ol>
</div>
</xsl:template>
</xsl:stylesheet>
编辑2 如果我用chrome检查页面,我得到这个:
<!--?xml version="1.0" encoding="utf-8"?-->
<!--?xml-stylesheet type="text/xsl" href="/XSLT/Templates.xslt"?-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<site>
<menu>
<sitemenu title="Edo" linkpath="/default.aspx" displayorder="0">
<sitemenu title="Infomation" linkpath="/information.aspx" displayorder="1"><sitemenu title="News" linkpath="/news.aspx" displayorder="2">
<sitemenu title="Menu" linkpath="/menu.aspx" displayorder="3">
<sitemenu title="Gallery" linkpath="/gallery.aspx" displayorder="4">
<sitemenu title="Contact" linkpath="/contact.aspx" displayorder="5">
<sitemenu title="Catering" linkpath="/catering.aspx" displayorder="6"></sitemenu>
</sitemenu>
</sitemenu>
</sitemenu>
</sitemenu>
</sitemenu>
</sitemenu>
</menu>
<slides>
<slideshow imgpath="/20160806_182949.jpg" displayorder="1">
<slideshow imgpath="/20160901_193700.jpg" displayorder="2">
<slideshow imgpath="/14202562_319650221718312_9092982335636542049_n.jpg" displayorder="3">
<slideshow imgpath="/14237745_319766348373366_7639011255502415884_n.jpg" displayorder="4">
</slideshow>
</slideshow>
</slideshow>
</slideshow>
</slides>
</site>
但在IE中我得到了这个
<xml:stylesheet></xml:stylesheet>
<Site>
<MENU>
<SiteMenu Title="Edo" Linkpath="/default.aspx" displayOrder="0" />
<SiteMenu Title="Infomation" Linkpath="/information.aspx" displayOrder="1" />
<SiteMenu Title="News" Linkpath="/news.aspx" displayOrder="2" />
<SiteMenu Title="Menu" Linkpath="/menu.aspx" displayOrder="3" />
<SiteMenu Title="Gallery" Linkpath="/gallery.aspx" displayOrder="4" />
<SiteMenu Title="Contact" Linkpath="/contact.aspx" displayOrder="5" />
<SiteMenu Title="Catering" Linkpath="/catering.aspx" displayOrder="6" />
</MENU>
<SLIDES>
<SlideShow imgPath="/20160806_182949.jpg" displayOrder="1" />
<SlideShow imgPath="/20160901_193700.jpg" displayOrder="2" />
<SlideShow imgPath="/14202562_319650221718312_9092982335636542049_n.jpg" displayOrder="3" />
<SlideShow imgPath="/14237745_319766348373366_7639011255502415884_n.jpg" displayOrder="4" />
</SLIDES>
</Site>
当一个控制台错误仍然显示“只允许一个根元素”时,很明显<site
是唯一的根元素。
编辑3
我在Chrome中的观点:
答案 0 :(得分:1)
您的样式表在第41行有一个额外的</div>
关闭标记。
修复后,会生成一个有效的HTML(虽然我没有看到任何呈现在屏幕上的内容,除非我删除了两个script
链接)。