Hejsa, og tak for dit indlæg! Det er dejligt at nogen gider at bruge lidt tid på det. Nedenfor har jeg indsat et par eksempler fra XML-dokumentet. Det er muligvis ikke verdens bedst opbyggede XML-dokument, men jeg håber det kan være med til at opklare mysteriet!
Mvh
Mikael
<SUBJECT ID="Q1">1. Why do my transforms fail using IE (or using my custom application)?
</SUBJECT>
<ANSWER>
Make sure you have installed the most recent MSXML processor. If you are using the parser that shipped with IE5
(or IE5.5), then you are using a parser that was based on the early working drafts, and does not much resemble
the XSLT of today. The XSLT list discusses today's version of XSLT, and you should probably be using it, too.
People on the list are not very friendly about helping with the old version, since it is so much different.
If you don't remember installing the latest processor, you probably didn't install it. For instructions on installing,
see the question
<INTERNAL_LINK SOURCE="Q5">"How do I install the updated MSXML?"</INTERNAL_LINK>
Chris Bayes provides an
<EXTERNAL-LINK SOURCE="
http://www.bayes.co.uk/xml/index.xml?/xml/utils/msxml_sniffer.htm"> MSXML Version Sniffer </EXTERNAL-LINK>
can tell you what version you have installed. Check to make sure you are actually using the latest processor.
It is possible that you are still using the old processor, even if you have installed the newer version of MSXML.
The first thing you should do is look at the top of this page, where there should be a message telling you what version
of MSXML you have installed on your machine. Another way to make sure you are using the correct processor is to try
running the transform from the
<EXTERNAL-LINK SOURCE="
http://www.netcrucible.com/xslt/xslt-tool.htm"> XSLT Test Tool </EXTERNAL-LINK>
If your transform works in this tool, but not in your application, chances are you are using the old processor.
See the question
<INTERNAL_LINK SOURCE="Q6">"I know I have installed the most recent MSXML"</INTERNAL_LINK>
to see how to fix your application. Check to make sure that the transform works in other XSLT processors. Again, the
<EXTERNAL-LINK SOURCE="
http://www.netcrucible.com/xslt/xslt-tool.htm">XSLT Test Tool</EXTERNAL-LINK>
is a good way to do this. You can test your transform against MSXML and any other XSLT processors that you have installed. I like to use
<EXTERNAL-LINK SOURCE="
http://users.iclway.co.uk/mhkay/saxon/"> Saxon</EXTERNAL-LINK>
and
<EXTERNAL-LINK SOURCE="
http://www.jclark.com/xml/xt.html"> Xt. </EXTERNAL-LINK> Using the other processors can often give you some very useful information about why your transform does not work as you expect, and knowing if any other parsers can do the transform correctly will be a key piece of information that will help others answer your questions. If you are testing only against the MSXML parsers, the
<EXTERNAL-LINK SOURCE="
http://www.xsldebugger.com/">XSL Debugger
</EXTERNAL-LINK>
has many more features than the XSLT Test Tool and is a better option. There is also a tool called
<EXTERNAL-LINK SOURCE="
http://www.vbxml.com/xpathvisualizer/default.asp">XPath Visualizer</EXTERNAL-LINK>
, which is very helpful when debugging XPath issues
within your transform. Once you have checked these things, you will have much better information to ask your question.</ANSWER>
<SUBJECT ID="Q2">2. How do I run XSLT transforms automatically from code?</SUBJECT>
<ANSWER>Here is some code that calls a transform against the recent
versions of MSXML:
<EXAMPLE>
Dim xmlSource As Object
Dim xmlXForm As Object
Set xmlSource = CreateObject("MSXML2.DOMDocument")
Set xmlXForm = CreateObject("MSXML2.DOMDocument")
xmlSource.validateOnParse = True
xmlXForm.validateOnParse = True
xmlSource.async = False
xmlXForm.async = False
xmlSource.loadXML Text3.Text ' This loads the text that I want to transform If
Err.Number <> 0 Then
strErr = Err.Description & vbCrLf
strErr = strErr & xmlSource.parseError.reason & " line: " &
xmlSource.parseError.Line & " col: " & xmlSource.parseError.linepos
& " text: " & xmlSource.parseError.srcText MsgBox strErr, vbCritical,
"Error loading the XML"
GoTo bail
End If
xmlXForm.loadXML Text1.Text ' This
loads the XSLT transform If Err.Number <> 0 Then
strErr = Err.Description & vbCrLf
strErr = strErr & xmlSource.parseError.reason & " line: "
& xmlSource.parseError.Line & " col: " &
xmlSource.parseError.linepos & " text: " &
xmlSource.parseError.srcText
MsgBox strErr, vbCritical, "Error loading the Transform"
GoTo bail
End If
Text2.Text = xmlSource.transformNode(xmlXForm) ' This transforms the data in
xmlSource If Err.Number <> 0 Then
strErr = Err.Description & vbCrLf
strErr = strErr & xmlSource.parseError.reason & " line: " &
xmlSource.parseError.Line & " col: " & xmlSource.parseError.linepos
& " text: " & xmlSource.parseError.srcText MsgBox strErr, vbCritical,
"Error executing the Transform"
GoTo bail
End If
Set xmlSource = Nothing Set
xmlXForm = Nothing
</EXAMPLE>
The code above is written in VB, but you can write using Java, JScript, VBScript, PerlScript, C++, or any other language that supports COM. The important thing about the above code sample is that we are using the CreateObject("MSXML2.DOMDocument") instead of
CreateObject("MSXML.DOMDocument"). Using "MSXML2" ensures that you call the newer XSLT processor, and not the working draft version from 1998. This example should also show you how to get detailed error information if your transform fails. One thing to note: if you are transforming unstructured data (non-XML), then you will probably want to set xmlSource.ValidateOnParse=False in the above
example. You probably don't want it to be telling you that your document is not well-formed XML when you already know that and don't care!</ANSWER>