Search code examples
xmlxsltxhtml

How to link up XML file with XSLT 3.0 file?


I have .xml file with data and .xsl with html table. How I can link up these files? (example is taken from documentation: https://www.w3.org/TR/xslt-30/#dt-raw-result) .xml:

<PERSONAE PLAY="OTHELLO">
    <TITLE>Dramatis Personae</TITLE>
    <PERSONA>DUKE OF VENICE</PERSONA>
    <PERSONA>BRABANTIO, a senator.</PERSONA>
    <PERSONA>Other Senators.</PERSONA>
    <PERSONA>GRATIANO, brother to Brabantio.</PERSONA>
    <PERSONA>LODOVICO, kinsman to Brabantio.</PERSONA>
    <PERSONA>OTHELLO, a noble Moor in the service of the Venetian state.</PERSONA>
    <PERSONA>CASSIO, his lieutenant.</PERSONA>
    <PERSONA>IAGO, his ancient.</PERSONA>
    <PERSONA>RODERIGO, a Venetian gentleman.</PERSONA>
    <PERSONA>MONTANO, Othello's predecessor in the government of Cyprus.</PERSONA>
    <PERSONA>Clown, servant to Othello. </PERSONA>
    <PERSONA>DESDEMONA, daughter to Brabantio and wife to Othello.</PERSONA>
    <PERSONA>EMILIA, wife to Iago.</PERSONA>
    <PERSONA>BIANCA, mistress to Cassio.</PERSONA>
    <PERSONA>Sailor, Messenger, Herald, Officers, 
             Gentlemen, Musicians, and Attendants.</PERSONA>
  </PERSONAE>

.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="3.0"
    expand-text="yes">
    
 <xsl:strip-space elements="PERSONAE"/>
 <xsl:template match="PERSONAE">
   <html>
     <head>
       <title>The Cast of {@PLAY}</title>
     </head>
     <body>
       <xsl:apply-templates/>
     </body>
   </html>
 </xsl:template>
 
 <xsl:template match="TITLE">
   <h1>{.}</h1>
 </xsl:template>
 
 <xsl:template match="PERSONA[count(tokenize(., ',') = 2]">
   <p><b>{substring-before(., ',')}</b>: {substring-after(., ',')}</p>
 </xsl:template> 

 <xsl:template match="PERSONA">
   <p><b>{.}</b></p>
 </xsl:template>

</xsl:stylesheet>

Thank you in advance!

I've seen about <?xml-stylesheet type="text/xsl" href="/file name/.xsl"?> but don't sure that it will be correct (when I launch .xml file, there is a blank site).


Solution

  • It depends on where you want to run the transformation, and how you want to control it. There are typically three options:

    (a) invoke the transformation from the command line, identifying both the source document and the stylesheet

    (b) invoke the transformation using an API from a programming language such as Java, C#, Javascript, or Python.

    (c) nominate the stylesheet within the XML file using the xml-stylesheet processing instruction, and then "load" the XML file using an environment (such as a browser) that recognises this. This approach has two drawbacks: (i) most such environments only recognise XSLT 1.0, and (ii) it makes it very difficult to use different stylesheets to process the same document on different occasions.

    In practice if you're using XSLT 3.0 then your first step is to choose your XSLT processor (there are only two that are actively supported, Altova and Saxon) and then look at its documentation to see how to invoke a transformation.