Avatar billede mexicano Nybegynder
05. juni 2002 - 10:10 Der er 4 kommentarer

Tabel i XSLT

Jeg forsøger desperat at konstruere en tabel med spørgsmål til venstre og svar til højre ved hjælp af en XSLT stylesheet. Jeg kan også godt få lavet tabellen, men når jeg åbner møget i IE viser den mig en tabel bestående at to kolonner med en celle i hver. I denne celle er så hhv. alle spørgsmål og alle svar indsat. Hvad er der der dog galt siden jeg ikke kan få lavet individuelle celler til hvert spørgsmål og svar? Nedenfor har jeg indsat det stylesheet jeg har lavet for at generere tabellen. Håber nogen kan/vil hjælpe!

Mvh

Mikael Andersen

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:template match="/">
  <html>
    <head>
      <title>Unofficial MSXML XSLT FAQ</title>
    </head>
    <body>
      <h1 style=" text-align:center; font-family:Arial Rounded MT bold;
color:maroon;
        background-color:silver">Unofficial MSXML XSLT FAQ</h1>
      <hr />
      <br />
      <table border="2" width="100%" cellpadding="20" >
        <thead>
        <tr style="font-weight:bold; font-family:Arial Rounded MT bold;
font-size:25; color:silver; background-color:maroon">
          <th>Questions</th>
          <th>Answers</th>
        </tr>
        </thead>
        <tbody>
          <xsl:apply-templates select="FAQ-LIST/FAQ/ANSWERS"/>
        </tbody>
      </table>
      </body>
    </html>
</xsl:template>

<xsl:template match="FAQ-LIST"/>

<!--Nedenstående template er mit forsøg på at generere ovennævnte tabel-->

<xsl:template match="FAQ-LIST/FAQ/ANSWERS">
<tr>
  <td width="30%" style="font-family:Arial Rounded MT bold; color:gray;
font-size:13">
  <xsl:for-each select="SUBJECT">
    <p>
    <xsl:value-of select="SUBJECT"/>
    <xsl:apply-templates />
    </p>
</xsl:for-each>
</td>
  <td width="70%" style="font-family:Arial Rounded MT Bold; color:maroon;
font-size:13;">
  <xsl:for-each select="ANSWER">
    <p>
    <xsl:value-of select="/ANSWER"/>
  <xsl:apply-templates />
  </p>
</xsl:for-each>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
Avatar billede janegil Nybegynder
06. juni 2002 - 12:14 #1
Jeg sitter med en følelse av at du heller skal bruke mere apply-templates og mindre for-each - paradigmeskifte, vet du.

Men hvordan ser XML'en din ut? Bare et lite eksempel, med to spørsmål og to svar?
Avatar billede mexicano Nybegynder
06. juni 2002 - 15:45 #2
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 &lt;&gt; 0 Then
            strErr = Err.Description &amp; vbCrLf
            strErr = strErr &amp; xmlSource.parseError.reason &amp; " line: " &amp;
              xmlSource.parseError.Line &amp; " col: " &amp; xmlSource.parseError.linepos
              &amp; " text: " &amp; 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 &lt;&gt; 0 Then
            strErr = Err.Description &amp; vbCrLf
            strErr = strErr &amp; xmlSource.parseError.reason &amp; " line: "
              &amp; xmlSource.parseError.Line &amp; " col: " &amp;
              xmlSource.parseError.linepos &amp; " text: " &amp;
            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 &lt;&gt; 0 Then
            strErr = Err.Description &amp; vbCrLf
              strErr = strErr &amp; xmlSource.parseError.reason &amp; " line: " &amp;
              xmlSource.parseError.Line &amp; " col: " &amp; xmlSource.parseError.linepos
              &amp; " text: " &amp; 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>
Avatar billede janegil Nybegynder
07. juni 2002 - 12:27 #3
Du har visst forstått problemet ditt: Ja, XML-dokumentet har en sematnisk struktur som ikke avspeiles i syntaks. Du og jeg er smart nok til å forstå at når spørsmål og svar kommer etter hverandre:
<q/><a/><q/><a/><q/><a/>
så hører et svar sammen med det spørsmålet det står til høyre for. Men så smart er jo ikke XML, den skal ha noe som
<faq>
  <qa><q/><a/></qa>
  <qa><q/><a/></qa>
  <qa><q/><a/></qa>
</faq>
for å 'forstå' hvilke svar som hører til hvilket spørsmål.

Radikal som jeg er, så anbefaler jeg først og fremst å endre XML-strukturen - XSLT kan brukes til dét også.

Hvis strukturen ligger fast, så skal du ha en template som matcher SUBJECT, og en som matcher ANSWER. Inne i SUBJECT-template skal du ha <apply-templates select="following-sibling::ANSWER"/>. Tror jeg nok. Men grisete XML gir grisete XSLT.
Avatar billede sagdjb Nybegynder
28. juni 2002 - 14:32 #4
Du har rodet dig ud i at smide det hele i samme celle... Hvordan du strukturer dit stylesheet er mig lige meget, men pricippet er at du laver:

...
<table>
<xsl:for-each select="FAQ-LIST/FAQ/ANSWERS">
  <tr>
    <td><xsl:value-of select="SUBJECT"/></td>
    <td><xsl:value-of select="ANSWER"/></td>
  </tr>
</xsl:for-each>
</table>
...

Ja ja du kan godt lave det som templates, jeg er lige glad. Men ideen er bare, at du ikke falder over din egen kode - KIS(s) (keep it simple).
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester