2010年5月1日星期六

XSLT中for-each的select语法问题

在一个程序中,要用到如下的XSL用于XSLT转换

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="utf-8"/>
<xsl:template match="/">
<messages>
    <xsl:for-each select="//tr[@bgcolor='#f3f2f1' and position()>=3 and position()<last()]">
        <message>
            <msgid>
                <xsl:value-of select="td[1]/div/input/@value"/>
            </msgid>
            <teacher>
                <xsl:value-of select="td[3]/div"/>
            </teacher>
            <content>
                <xsl:value-of select="td[4]/div"/>
            </content>
            <timestamp>
                <xsl:value-of select="td[6]/div"/>
            </timestamp>
        </message>
    </xsl:for-each>
</messages>
</xsl:template>
</xsl:stylesheet>

在.NET中报错,出错信息为:

未处理 System.Xml.Xsl.XslLoadException
  Message="XSLT 编译错误。"
  Source="System.Data.SqlXml"
  LineNumber=6
  LinePosition=81
  SourceUri="file:///D:/myprog/C%23/jxlltEasy/jxlltEasy/bin/Debug/msg.xsl"
  StackTrace:
       在 System.Xml.Xsl.Xslt.XsltLoader.LoadStylesheet(XmlReader reader, Boolean include)
       在 System.Xml.Xsl.Xslt.XsltLoader.Load(Compiler compiler, Object stylesheet, XmlResolver xmlResolver)
       在 System.Xml.Xsl.Xslt.Compiler.Compile(Object stylesheet, XmlResolver xmlResolver, QilExpression& qil)
       在 System.Xml.Xsl.XslCompiledTransform.CompileXsltToQil(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver)
       在 System.Xml.Xsl.XslCompiledTransform.LoadInternal(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver)
       在 System.Xml.Xsl.XslCompiledTransform.Load(String stylesheetUri)
       在 jxlltEasy.jxlltForm.btnRefresh_Click(Object sender, EventArgs e) 位置 D:\myprog\C#\jxlltEasy\jxlltEasy\jxlltForm.cs:行号 120
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.Run(Form mainForm)
       在 jxlltEasy.Program.Main() 位置 D:\myprog\C#\jxlltEasy\jxlltEasy\Program.cs:行号 18
       在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.Xml.XmlException
       Message="“<”(十六进制值 0x3C)是无效的属性字符。 行 6,位置 81。"

       Source="System.Xml"
       LineNumber=6
       LinePosition=81
       SourceUri="file:///D:/myprog/C%23/jxlltEasy/jxlltEasy/bin/Debug/msg.xsl"
       StackTrace:
            在 System.Xml.XmlTextReaderImpl.Throw(Exception e)
            在 System.Xml.XmlTextReaderImpl.Throw(String res, String[] args)
            在 System.Xml.XmlTextReaderImpl.Throw(Int32 pos, String res, String[] args)
            在 System.Xml.XmlTextReaderImpl.ParseAttributeValueSlow(Int32 curPos, Char quoteChar, NodeData attr)
            在 System.Xml.XmlTextReaderImpl.ParseAttributes()
            在 System.Xml.XmlTextReaderImpl.ParseElement()
            在 System.Xml.XmlTextReaderImpl.ParseElementContent()
            在 System.Xml.XmlTextReaderImpl.Read()
            在 System.Xml.Xsl.Xslt.XsltInput.ReadNextSiblingHelper()
            在 System.Xml.Xsl.Xslt.XsltInput.ReadNextSibling()
            在 System.Xml.Xsl.Xslt.XsltInput.MoveToFirstChildAny()
            在 System.Xml.Xsl.Xslt.XsltInput.MoveToFirstChild()
            在 System.Xml.Xsl.Xslt.XsltLoader.LoadInstructions(List`1 content, InstructionFlags flags)
            在 System.Xml.Xsl.Xslt.XsltLoader.LoadLiteralResultElement(Boolean asStylesheet)
            在 System.Xml.Xsl.Xslt.XsltLoader.LoadInstructions(List`1 content, InstructionFlags flags)
            在 System.Xml.Xsl.Xslt.XsltLoader.LoadTemplate(NsDecl stylesheetNsList)
            在 System.Xml.Xsl.Xslt.XsltLoader.LoadRealStylesheet()
            在 System.Xml.Xsl.Xslt.XsltLoader.LoadDocument()
            在 System.Xml.Xsl.Xslt.XsltLoader.LoadStylesheet(XmlReader reader, Boolean include)
       InnerException:

在Java中,出错信息为:

Fatal Error] :6:80: The value of attribute "select" associated with an element type "xsl:for-each" must not contain the '<' character.
Exception in thread "main" org.xml.sax.SAXParseException: The value of attribute "select" associated with an element type "xsl:for-each" must not contain the '<' character.
    at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
    at cn.ldsoft.xpath.XMLHelper.parseXMLFromInputSource(XMLHelper.java:70)
    at cn.ldsoft.xpath.XMLHelper.parseXMLFromFile(XMLHelper.java:62)
    at cn.ldsoft.simple.Test.main(Test.java:22)

通过查阅资料得知,

合法的过滤运算符:
  • =  (等于)
  • != (不等于)
  • &lt; (小于)
  • &gt; (大于)

将position()<last()改为position() &lt; last(), 问题解决

没有评论:

发表评论