如何将XML文件与XSLT 3.0文件关联?

huangapple go评论63阅读模式
英文:

How to link up XML file with XSLT 3.0 file?

问题

我有一个包含数据的.xml文件和一个包含HTML表格的.xsl文件。如何将这些文件链接起来?

.xml文件示例(摘自文档https://www.w3.org/TR/xslt-30/#dt-raw-result):

<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>

感谢您的提前帮助!

我看到关于<?xml-stylesheet type="text/xsl" href="/file name/.xsl"?>的内容,但不确定它是否正确(当我启动.xml文件时,只显示空白页面)。

英文:

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).

答案1

得分: 0

(a) 从命令行调用转换,识别源文档和样式表。

(b) 使用来自编程语言(如Java、C#、JavaScript或Python)的API调用转换。

(c) 在XML文件中使用xml-stylesheet处理指令指定样式表,然后使用能够识别此处理的环境(例如浏览器)"加载" XML 文件。这种方法有两个缺点:(i) 大多数此类环境只能识别XSLT 1.0,(ii) 在不同场合处理相同文档时很难使用不同的样式表。

实际上,如果你使用XSLT 3.0,那么第一步是选择XSLT处理器(只有两种是活跃支持的,Altova和Saxon),然后查看其文档以了解如何调用转换。

英文:

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.

huangapple
  • 本文由 发表于 2023年2月14日 06:52:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定