2012-10-18 40 views
1

我要修改的XSLT 1.0在所附XSL文件變換,生成XML文件中的編碼表中的一個HTML版本(也附後)。生成的HTML輸出文件必須包含保留所有內容的XML文件中定義的表格,並且在Web瀏覽器中呈現時交替的數據行分配不同的背景色彩。此外,生成的HTML輸出必須是有效的XHTML 1.1,並且必須單次生成,即run.bat只執行一次。請幫助我。預先感謝您,我真的很感激!XML通過XSLT爲HTML變換修飾

XSL文件:

<?xml version="1.0"?> 
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" xmlns:saxon="http://icl.com/saxon" xmlns:date="http://exslt.org/dates-and-times" extension-element-prefixes="saxon date" version="1.0"> 
    <xsl:output method="text" encoding="UTF-8" indent="no"/> 
    <xsl:template match="Item">Hello world</xsl:template> 
</xsl:transform> 

XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<Item TextType="BodyMatter" SchemaVersion="2.0" id="AaAF_Freelance_Test_1" DiscussionAlias="Discussion" SessionAlias="Chapter" SecondColour="Pantone326C" ThirdColour="Pantone2945C" FourthColour="Pantone272C" Logo="colour" Rendering="VLE Preview" Template="Wide_Margin_Reduced_A4_Unnumbered_new_design_v10_release1"> 
    <CourseCode>AaAF</CourseCode> 
    <CourseTitle>Accessible and alternative formats</CourseTitle> 
    <ItemID/> 
    <ItemTitle>AaAF: Freelance XSLT test document 1</ItemTitle> 
    <Unit> 
    <UnitID/> 
    <UnitTitle/> 
    <ByLine/> 
    <Session> 
     <Title>Freelance XSLT test – constructing tables</Title> 
     <Paragraph>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec quis elit sapien. Duis sit amet interdum lectus. Sed faucibus, mi quis dapibus consequat, nisi nunc semper erat, eu pretium enim urna porta diam. Suspendisse mi nisi, adipiscing in semper ac, viverra vitae nisl. Nulla facilisi. Nullam luctus porttitor velit, quis euismod dui commodo eu. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris arcu est, rhoncus interdum bibendum vel, commodo et justo.</Paragraph> 
     <Table> 
     <TableHead>Table of analytical techniques</TableHead> 
     <tbody> 
      <tr> 
      <th class="ColumnHeadLeft">Analytical technique</th> 
      <th class="ColumnHeadLeft">Application</th> 
      </tr> 
      <tr> 
      <td class="TableLeft">IR reflectography</td> 
      <td class="TableLeft">Characterisation of painting techniques, in panel and wall paintings, through the identification of underdrawings; identification of undocumented interventions; characterisation of pigments.</td> 
      </tr> 
      <tr> 
      <td class="TableLeft">FT-IR spectroscopy</td> 
      <td class="TableLeft"><i>In situ</i> non-destructive identification of alterations on stones, mortars and metals, identification of pigments and binders in paintings; identification of undocumented restorations.</td> 
      </tr> 
      <tr> 
      <td class="TableLeft">Raman spectroscopy</td> 
      <td class="TableLeft"><i>In situ</i> non-destructive identification of alterations on stones, mortars and metals; identification of pigments in wall paintings. </td> 
      </tr> 
      <tr> 
      <td class="TableLeft">XRF</td> 
      <td class="TableLeft">Elemental analyses of stones, metals, wall and easel paintings, and other objects. </td> 
      </tr> 
      <tr> 
      <td class="TableLeft">Vis-NIR spectroscopy</td> 
      <td class="TableLeft">Colorimetric measurements and, possibly, identification of pigments. Measurement of possible colour changing after restoration.</td> 
      </tr> 
      <tr> 
      <td class="TableLeft">UV-vis spectroscopy</td> 
      <td class="TableLeft"><i>In situ</i> non-destructive identification of the presence of organic substances. </td> 
      </tr> 
      <tr> 
      <td class="TableLeft">Fluorimetry</td> 
      <td class="TableLeft"><i>In situ</i> detection of the distribution of organic molecules</td> 
      </tr> 
      <tr> 
      <td class="TableLeft">Drilling resistance measurement </td> 
      <td class="TableLeft">Hardness of rocks used for monuments and sculptures</td> 
      </tr> 
     </tbody> 
     </Table> 
    </Session> 
    </Unit> 
</Item> 

run.bat文件:

java -jar ou_saxon.jar -o output1.html input.xml convert.xsl 
pause 

回答

2

這種轉變

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="/"> 
    <xsl:apply-templates select="/*/*/*/Table"/> 
</xsl:template> 

<xsl:template match="Table"> 
    <table> 
     <xsl:apply-templates select="node()|@*"/> 
    </table> 
</xsl:template> 

<xsl:template match="TableHead"> 
    <th><xsl:apply-templates select="node()|@*"/></th> 
</xsl:template> 

<xsl:template match="tr[not(th)][position() mod 2 = 1]"> 
    <tr class="odd"> 
    <xsl:apply-templates select="node()|@*"/> 
    </tr> 
</xsl:template> 

<xsl:template match="tr[not(th)][position() mod 2 = 0]"> 
    <tr class="even"> 
    <xsl:apply-templates select="node()|@*"/> 
    </tr> 
</xsl:template> 
</xsl:stylesheet> 

時所提供的XML文檔應用:

<Item TextType="BodyMatter" SchemaVersion="2.0" id="AaAF_Freelance_Test_1" DiscussionAlias="Discussion" SessionAlias="Chapter" SecondColour="Pantone326C" ThirdColour="Pantone2945C" FourthColour="Pantone272C" Logo="colour" Rendering="VLE Preview" Template="Wide_Margin_Reduced_A4_Unnumbered_new_design_v10_release1"> 
    <CourseCode>AaAF</CourseCode> 
    <CourseTitle>Accessible and alternative formats</CourseTitle> 
    <ItemID/> 
    <ItemTitle>AaAF: Freelance XSLT test document 1</ItemTitle> 
    <Unit> 
     <UnitID/> 
     <UnitTitle/> 
     <ByLine/> 
     <Session> 
      <Title>Freelance XSLT test – constructing tables</Title> 
      <Paragraph>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec quis elit sapien. Duis sit amet interdum lectus. Sed faucibus, mi quis dapibus consequat, nisi nunc semper erat, eu pretium enim urna porta diam. Suspendisse mi nisi, adipiscing in semper ac, viverra vitae nisl. Nulla facilisi. Nullam luctus porttitor velit, quis euismod dui commodo eu. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris arcu est, rhoncus interdum bibendum vel, commodo et justo.</Paragraph> 
      <Table> 
       <TableHead>Table of analytical techniques</TableHead> 
       <tbody> 
        <tr> 
         <th class="ColumnHeadLeft">Analytical technique</th> 
         <th class="ColumnHeadLeft">Application</th> 
        </tr> 
        <tr> 
         <td class="TableLeft">IR reflectography</td> 
         <td class="TableLeft">Characterisation of painting techniques, in panel and wall paintings, through the identification of underdrawings; identification of undocumented interventions; characterisation of pigments.</td> 
        </tr> 
        <tr> 
         <td class="TableLeft">FT-IR spectroscopy</td> 
         <td class="TableLeft"> 
          <i>In situ</i> non-destructive identification of alterations on stones, mortars and metals, identification of pigments and binders in paintings; identification of undocumented restorations. 
         </td> 
        </tr> 
        <tr> 
         <td class="TableLeft">Raman spectroscopy</td> 
         <td class="TableLeft"> 
          <i>In situ</i> non-destructive identification of alterations on stones, mortars and metals; identification of pigments in wall paintings. 
         </td> 
        </tr> 
        <tr> 
         <td class="TableLeft">XRF</td> 
         <td class="TableLeft">Elemental analyses of stones, metals, wall and easel paintings, and other objects. </td> 
        </tr> 
        <tr> 
         <td class="TableLeft">Vis-NIR spectroscopy</td> 
         <td class="TableLeft">Colorimetric measurements and, possibly, identification of pigments. Measurement of possible colour changing after restoration.</td> 
        </tr> 
        <tr> 
         <td class="TableLeft">UV-vis spectroscopy</td> 
         <td class="TableLeft"> 
          <i>In situ</i> non-destructive identification of the presence of organic substances. 
         </td> 
        </tr> 
        <tr> 
         <td class="TableLeft">Fluorimetry</td> 
         <td class="TableLeft"> 
          <i>In situ</i> detection of the distribution of organic molecules 
         </td> 
        </tr> 
        <tr> 
         <td class="TableLeft">Drilling resistance measurement </td> 
         <td class="TableLeft">Hardness of rocks used for monuments and sculptures</td> 
        </tr> 
       </tbody> 
      </Table> 
     </Session> 
    </Unit> 
</Item> 

產生想要的,正確的結果

<table> 
    <th>Table of analytical techniques</th> 
    <tbody> 
     <tr> 
     <th class="ColumnHeadLeft">Analytical technique</th> 
     <th class="ColumnHeadLeft">Application</th> 
     </tr> 
     <tr class="odd"> 
     <td class="TableLeft">IR reflectography</td> 
     <td class="TableLeft">Characterisation of painting techniques, in panel and wall paintings, through the identification of underdrawings; identification of undocumented interventions; characterisation of pigments.</td> 
     </tr> 
     <tr class="even"> 
     <td class="TableLeft">FT-IR spectroscopy</td> 
     <td class="TableLeft"> 
      <i>In situ</i> non-destructive identification of alterations on stones, mortars and metals, identification of pigments and binders in paintings; identification of undocumented restorations. 
         </td> 
     </tr> 
     <tr class="odd"> 
     <td class="TableLeft">Raman spectroscopy</td> 
     <td class="TableLeft"> 
      <i>In situ</i> non-destructive identification of alterations on stones, mortars and metals; identification of pigments in wall paintings. 
         </td> 
     </tr> 
     <tr class="even"> 
     <td class="TableLeft">XRF</td> 
     <td class="TableLeft">Elemental analyses of stones, metals, wall and easel paintings, and other objects. </td> 
     </tr> 
     <tr class="odd"> 
     <td class="TableLeft">Vis-NIR spectroscopy</td> 
     <td class="TableLeft">Colorimetric measurements and, possibly, identification of pigments. Measurement of possible colour changing after restoration.</td> 
     </tr> 
     <tr class="even"> 
     <td class="TableLeft">UV-vis spectroscopy</td> 
     <td class="TableLeft"> 
      <i>In situ</i> non-destructive identification of the presence of organic substances. 
         </td> 
     </tr> 
     <tr class="odd"> 
     <td class="TableLeft">Fluorimetry</td> 
     <td class="TableLeft"> 
      <i>In situ</i> detection of the distribution of organic molecules 
         </td> 
     </tr> 
     <tr class="even"> 
     <td class="TableLeft">Drilling resistance measurement </td> 
     <td class="TableLeft">Hardness of rocks used for monuments and sculptures</td> 
     </tr> 
    </tbody> 
</table> 

請注意

改造只是增加eac爲「odd」或「even」的類屬性h tr。這假定一個css文件也將被引用,並且它具有爲.odd.even定義的適當的CSS屬性。

如果不是這種情況,可以改變轉換來生成直接格式。

例如,我們couldhave在兩個模板,分別爲:

<tr bgcolor="white"> 

<tr bgcolor="#87b1f1"> 

最後,如果我們希望輸出是在XHTML命名空間中,我們將進一步modufy改造這樣

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns="http://www.w3.org/1999/xhtml"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="/"> 
    <xsl:apply-templates select="/*/*/*/Table"/> 
</xsl:template> 

<xsl:template match="*"> 
    <xsl:element name="{name()}" namespace="http://www.w3.org/1999/xhtml"> 
    <xsl:copy-of select="namespace::*"/> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="Table"> 
    <table> 
     <xsl:apply-templates select="node()|@*"/> 
    </table> 
</xsl:template> 

<xsl:template match="TableHead"> 
    <th><xsl:apply-templates select="node()|@*"/></th> 
</xsl:template> 

<xsl:template match="tr[not(th)][position() mod 2 = 1]"> 
    <tr bgcolor="white"> 
    <xsl:apply-templates select="node()|@*"/> 
    </tr> 
</xsl:template> 

<xsl:template match="tr[not(th)][position() mod 2 = 0]"> 
    <tr bgcolor="#87b1f1"> 
    <xsl:apply-templates select="node()|@*"/> 
    </tr> 
</xsl:template> 
</xsl:stylesheet> 

當同一個XML文檔與這種轉變處理,現在的結果是在XHTML命名空間

<table xmlns="http://www.w3.org/1999/xhtml"> 
    <th>Table of analytical techniques</th> 
    <tbody> 
     <tr> 
     <th class="ColumnHeadLeft">Analytical technique</th> 
     <th class="ColumnHeadLeft">Application</th> 
     </tr> 
     <tr bgcolor="white"> 
     <td class="TableLeft">IR reflectography</td> 
     <td class="TableLeft">Characterisation of painting techniques, in panel and wall paintings, through the identification of underdrawings; identification of undocumented interventions; characterisation of pigments.</td> 
     </tr> 
     <tr bgcolor="#87b1f1"> 
     <td class="TableLeft">FT-IR spectroscopy</td> 
     <td class="TableLeft"> 
      <i>In situ</i> non-destructive identification of alterations on stones, mortars and metals, identification of pigments and binders in paintings; identification of undocumented restorations. 
         </td> 
     </tr> 
     <tr bgcolor="white"> 
     <td class="TableLeft">Raman spectroscopy</td> 
     <td class="TableLeft"> 
      <i>In situ</i> non-destructive identification of alterations on stones, mortars and metals; identification of pigments in wall paintings. 
         </td> 
     </tr> 
     <tr bgcolor="#87b1f1"> 
     <td class="TableLeft">XRF</td> 
     <td class="TableLeft">Elemental analyses of stones, metals, wall and easel paintings, and other objects. </td> 
     </tr> 
     <tr bgcolor="white"> 
     <td class="TableLeft">Vis-NIR spectroscopy</td> 
     <td class="TableLeft">Colorimetric measurements and, possibly, identification of pigments. Measurement of possible colour changing after restoration.</td> 
     </tr> 
     <tr bgcolor="#87b1f1"> 
     <td class="TableLeft">UV-vis spectroscopy</td> 
     <td class="TableLeft"> 
      <i>In situ</i> non-destructive identification of the presence of organic substances. 
         </td> 
     </tr> 
     <tr bgcolor="white"> 
     <td class="TableLeft">Fluorimetry</td> 
     <td class="TableLeft"> 
      <i>In situ</i> detection of the distribution of organic molecules 
         </td> 
     </tr> 
     <tr bgcolor="#87b1f1"> 
     <td class="TableLeft">Drilling resistance measurement </td> 
     <td class="TableLeft">Hardness of rocks used for monuments and sculptures</td> 
     </tr> 
    </tbody> 
</table> 
+0

哇!非常感謝你,@Dimitre。你是明星! 的確,任務帶有一些CSS文件(雖然我的經理說他們是可選的)。因此,你的猜測恰恰是現貨! 雖然快速的問題,什麼是run.bat文件都是關於?我的意思是,它在這方面扮演什麼角色? 乾杯哥們,我欠你一品脫! –

+0

@LewisJones,不客氣。至於run.bat文件,它包含調用Saxon XSLT處理器的命令行實用程序調用(否則必須在DOS窗口中手動輸入),指定源XML文件,XSLT文件和輸出應該寫入轉換結果的文件。 –

+0

再次感謝Dimitre。我現在有一個小問題:爲了將標題「分析技術表」與左側和右側表格標題「分析技術」和「應用程序」分開,我需要做些什麼?當表格出現在瀏覽器中時,該標題需要從兩個表格中脫穎而出。我試過一切都無濟於事。我甚至嘗試過CSS align-center代碼,但它根本無法工作。請有任何想法嗎? –