C# 使用C将XML转换为HTML时出现异常#
我有以下XML:C# 使用C将XML转换为HTML时出现异常#,c#,xml,xslt-1.0,C#,Xml,Xslt 1.0,我有以下XML: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="yes" version="4.01" doctype-system="http://www.w3.org/TR/xhtml1/DTD
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" version="4.01"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
doctype-public="//W3C//DTD XHTML 1.0 Transitional//EN"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<xsl:attribute name="lang">
<xsl:value-of select="//Settings//LanguageCode"/>
</xsl:attribute>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<link rel="stylesheet" type="text/css">
<xsl:attribute name="href">
<xsl:value-of select="//Settings/Template/@Style"/>
</xsl:attribute>
</link>
<title>
<!--<xsl:value-of select="//Labels/ReportTitleWorksheets"/>-->
Assignment Slips
</title>
<style type="text/css">
@import url('<xsl:text>2019/</xsl:text><xsl:value-of select="//Settings/Template/@Style"/>');
</style>
</head>
<body>
<xsl:for-each select="AssignmentSlips/Page">
<div class="containerPage">
<xsl:if test="@PageBreakBefore=1">
<br style="page-break-before: always;"/>
</xsl:if>
<xsl:for-each select="StudentSlip">
<div class="containerSlip">
<img alt="s89" width="323px" height="429px">
<xsl:attribute name="src">
<xsl:text>2019\</xsl:text>
<xsl:value-of select="//Settings/Template"/>
</xsl:attribute>
</img>
<div class="fieldName">
<xsl:attribute name="dir">
<xsl:value-of select="//Settings/Direction"/>
</xsl:attribute>
<xsl:value-of select="Student"/>
</div>
<div class="fieldAssisant">
<xsl:attribute name="dir">
<xsl:value-of select="//Settings/Direction"/>
</xsl:attribute>
<xsl:value-of select="Assistant"/>
</div>
<div class="fieldDate">
<xsl:attribute name="dir">
<xsl:value-of select="//Settings/Direction"/>
</xsl:attribute>
<xsl:value-of select="Date"/>
</div>
<div class="fieldCounsel">
<xsl:choose>
<xsl:when test="@ItemPosition='1'">
<xsl:text>1st: </xsl:text>
</xsl:when>
<xsl:when test="@ItemPosition='2'">
<xsl:text>2nd: </xsl:text>
</xsl:when>
<xsl:when test="@ItemPosition='3'">
<xsl:text>3rd: </xsl:text>
</xsl:when>
</xsl:choose>
<xsl:attribute name="dir">
<xsl:value-of select="//Settings/Direction"/>
</xsl:attribute>
<xsl:value-of select="StudyPoint"/>
</div>
<xsl:choose>
<xsl:when test="Assignment=1">
<div class="checkBibleReading">✓</div>
</xsl:when>
<xsl:when test="Assignment=2">
<div class="checkInitialCall">✓</div>
<div class="fieldInitialCallIndex">
<xsl:choose>
<xsl:when test="@ItemPosition='1'">
<xsl:text>#1</xsl:text>
</xsl:when>
<xsl:when test="@ItemPosition='2'">
<xsl:text>#2 </xsl:text>
</xsl:when>
<xsl:when test="@ItemPosition='3'">
<xsl:text>#3</xsl:text>
</xsl:when>
</xsl:choose>
</div>
</xsl:when>
<xsl:when test="Assignment=3">
<div class="checkFirstReturnVisit">✓</div>
<div class="fieldFirstReturnVisitIndex">
<xsl:choose>
<xsl:when test="@ItemPosition='1'">
<xsl:text>#1</xsl:text>
</xsl:when>
<xsl:when test="@ItemPosition='2'">
<xsl:text>#2 </xsl:text>
</xsl:when>
<xsl:when test="@ItemPosition='3'">
<xsl:text>#3</xsl:text>
</xsl:when>
</xsl:choose>
</div>
</xsl:when>
<xsl:when test="Assignment=4">
<div class="checkSecondReturnVisit">✓</div>
</xsl:when>
<xsl:when test="Assignment=5">
<div class="checkThirdReturnVisit">✓</div>
</xsl:when>
<xsl:when test="Assignment=6">
<div class="checkBibleStudy">✓</div>
</xsl:when>
<xsl:when test="Assignment=7">
<div class="checkTalk">✓</div>
</xsl:when>
<xsl:when test="Assignment=0">
<div class="checkOther">✓</div>
<div class="fieldOther">
<xsl:attribute name="dir">
<xsl:value-of select="//Settings/Direction"/>
</xsl:attribute>
<xsl:value-of select="Other"/>
</div>
</xsl:when>
</xsl:choose>
<xsl:choose>
<xsl:when test="Location=1">
<div class="checkMainHall">✓</div>
</xsl:when>
<xsl:when test="Location=2">
<div class="checkAuxClass1">✓</div>
</xsl:when>
<xsl:when test="Location=3">
<div class="checkAuxClass2">✓</div>
</xsl:when>
</xsl:choose>
</div>
</xsl:for-each>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我得到了一个例外:
<LogEntry Date="2019-06-09 16:04:59" Severity="Exception" Source="MSAToolsLibrary.MSAToolsLibraryClass.TransformXMLToHTML" ThreadId="1">
<Exception Type="System.Xml.Xsl.XslTransformException" Source="System.Xml.Xsl.Runtime.XmlQueryOutput.ThrowInvalidStateError">
<Message>Attribute and namespace nodes cannot be added to the parent element after a text, comment, pi, or sub-element node has already been added.</Message>
<StackTrace> at System.Xml.Xsl.Runtime.XmlQueryOutput.ThrowInvalidStateError(XPathNodeType constructorType)
at System.Xml.Xsl.Runtime.XmlQueryOutput.ConstructInEnumAttrs(XPathNodeType rootType)
at System.Xml.Xsl.Runtime.XmlQueryOutput.WriteStartAttribute(String prefix, String localName, String ns)
at <xsl:template name="compiler:generated">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current)
at <xsl:template match="/">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current)
at Root(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
at Execute(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results)
at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer)
at System.Xml.Xsl.XslCompiledTransform.Transform(String inputUri, XsltArgumentList arguments, TextWriter results)
at MSAToolsLibrary.MSAToolsLibraryClass.TransformXMLToHTML(String strTransformXSLPath, String strScheduleXMLPath, String strScheduleHTMLPath)</StackTrace>
</Exception>
</LogEntry>
在已添加文本、注释、pi或子元素节点后,无法将属性和命名空间节点添加到父元素。
在System.Xml.Xsl.Runtime.XmlQueryOutput.ThrowInvalidStateError处(XPathNodeType构造函数类型)
位于System.Xml.Xsl.Runtime.XmlQueryOutput.ConstructInEnumAttrs(XPathNodeType根类型)
位于System.Xml.Xsl.Runtime.XmlQueryOutput.WriteStarAttribute(字符串前缀、字符串localName、字符串ns)
在xsl:template name=“compiler:generated”(XmlQueryRuntime{urn:schemas-microsoft-com:xslt-debug}运行时,XPathNavigator{urn:schemas-microsoft-com:xslt-debug}当前)
在xsl:template match=“/”(XmlQueryRuntime{urn:schemas microsoft com:xslt debug}运行时,XPathNavigator{urn:schemas microsoft com:xslt debug}当前)
在根目录下(XmlQueryRuntime{urn:schemas-microsoft-com:xslt-debug}运行时)
在执行时(XmlQueryRuntime{urn:schemas microsoft com:xslt debug}运行时)
在System.Xml.Xsl.XmlILCommand.Execute(对象defaultDocument、XmlResolver数据源、XslTargetList argumentList、XmlSequenceWriter结果)
在System.Xml.Xsl.XmlILCommand.Execute(对象defaultDocument、XmlResolver数据源、XslTargetList argumentList、XmlWriter)
在System.Xml.Xsl.xslcomiledtransform.Transform(字符串输入URI、XsltArgumentList参数、TextWriter结果)
在MSAToolsLibrary.MSAToolsLibraryClass.TransformXMLToHTML(字符串strTransferorMXSLPath、字符串strScheduleXMLPath、字符串strScheduleHTMLPath)
如何防止此问题?您的XSLT有151行。您最好删除指令块,直到找到问题(或至少找到问题区域)。此外,一些工具(例如)将提供一个行号以及错误消息 在这里转储整个代码并期望其他人调试它不是正确的做法。此外,如果没有XML输入,就不可能调试它 也就是说,我在XSLT的第57-73行看到了这一点:
<div class="fieldCounsel">
<xsl:choose>
<xsl:when test="@ItemPosition='1'">
<xsl:text>1st: </xsl:text>
</xsl:when>
<xsl:when test="@ItemPosition='2'">
<xsl:text>2nd: </xsl:text>
</xsl:when>
<xsl:when test="@ItemPosition='3'">
<xsl:text>3rd: </xsl:text>
</xsl:when>
</xsl:choose>
<xsl:attribute name="dir">
<xsl:value-of select="//Settings/Direction"/>
</xsl:attribute>
<xsl:value-of select="StudyPoint"/>
</div>
第一:
第二:
第三:
这里创建一个div
元素,并用一些文本填充它(如果其中一个测试返回true)。然后尝试添加一个属性。这是不允许的,因为您的错误消息试图告诉您:
在已添加文本、注释、pi或子元素节点后,无法将属性和命名空间节点添加到父元素
这符合:
以下是所有错误:
- 在向元素添加子元素后向元素添加属性李>
我没有检查你的代码超过这一点。可能有更多类似或其他错误。您的XSLT有151行。您最好删除指令块,直到找到问题(或至少找到问题区域)。此外,一些工具(例如)将提供一个行号以及错误消息 在这里转储整个代码并期望其他人调试它不是正确的做法。此外,如果没有XML输入,就不可能调试它 也就是说,我在XSLT的第57-73行看到了这一点:
<div class="fieldCounsel">
<xsl:choose>
<xsl:when test="@ItemPosition='1'">
<xsl:text>1st: </xsl:text>
</xsl:when>
<xsl:when test="@ItemPosition='2'">
<xsl:text>2nd: </xsl:text>
</xsl:when>
<xsl:when test="@ItemPosition='3'">
<xsl:text>3rd: </xsl:text>
</xsl:when>
</xsl:choose>
<xsl:attribute name="dir">
<xsl:value-of select="//Settings/Direction"/>
</xsl:attribute>
<xsl:value-of select="StudyPoint"/>
</div>
第一:
第二:
第三:
这里创建一个div
元素,并用一些文本填充它(如果其中一个测试返回true)。然后尝试添加一个属性。这是不允许的,因为您的错误消息试图告诉您:
在已添加文本、注释、pi或子元素节点后,无法将属性和命名空间节点添加到父元素
这符合:
以下是所有错误:
- 在向元素添加子元素后向元素添加属性李>
我没有检查你的代码超过这一点。可能还有更多类似或其他错误。谢谢。我接受你的观点。那么,如果不允许,那么决议是什么?我知道您可以这样做,因为它在IE浏览器中工作。在给定的示例中,您只需移动
xsl:attribute
指令,使其成为div
(在xsl:choose
之前)的第一个子项。谢谢。我接受你的观点。那么,如果不允许,那么决议是什么?我知道您可以这样做,因为它在IE浏览器中工作。在给定的示例中,您只需移动xsl:attribute
指令,使其成为div
的第一个子项(在xsl:choose
之前)。