为什么使用XSL将XML / CSS / JPG的转换转为HTML不起作用?

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

Why is this transform of XML / CSS / JPG using XSL to HTML not working?

问题

不确定如何在线测试环境中解决此问题。

测试XSL脚本:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes" version="4.01"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
    doctype-public="//W3C//DTD XHTML 1.0 Transitional//EN"/>
  <xsl:template match="/">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <xsl:attribute name="lang">
        <xsl:value-of select="//Settings//LanguageCode"/>
      </xsl:attribute>
      <head>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
        <link rel="stylesheet" type="text/css">
          <xsl:attribute name="href">
            <xsl:value-of select="//Settings/Template/@Style"/>
          </xsl:attribute>
        </link>
        <title>
          <!--xsl:value-of select="//Labels/ReportTitleWorksheets"-->
          Assignment Slips
        </title>
        <style type="text/css">
          @import url('2020/<xsl:text><xsl:value-of select="//Settings/Template/@Style"/></xsl:text>');
        </style>
      </head>
      <body>
        <xsl:for-each select="AssignmentSlips/Page">
          <div class="containerPage">
            <xsl:if test="@PageBreakBefore=1">
              <br style="page-break-before: always;"/>
            </xsl:if>
            <xsl:for-each select="StudentSlip">
              <div class="containerSlip">
                <img alt="s89" width="323px" height="429px">
                  <xsl:attribute name="src">
                    <xsl:text>2020\</xsl:text>
                    <xsl:value-of select="//Settings/Template"/>
                  </xsl:attribute>
                </img>
                <div class="fieldName">
                  <xsl:attribute name="dir">
                    <xsl:value-of select="//Settings/Direction"/>
                  </xsl:attribute>
                  <xsl:value-of select="Student"/>
                </div>
                <div class="fieldAssisant">
                  <xsl:attribute name="dir">
                    <xsl:value-of select="//Settings/Direction"/>
                  </xsl:attribute>
                  <xsl:value-of select="Assistant"/>
                </div>
                <div class="fieldDate">
                  <xsl:attribute name="dir">
                    <xsl:value-of select="//Settings/Direction"/>
                  </xsl:attribute>
                  <xsl:value-of select="Date"/>
                </div>
                <div class="fieldCounsel">
                  <xsl:choose>
                    <xsl:when test="@ItemPosition='1' and Assistant!=''">
                      <xsl:text>1st: </xsl:text>
                    </xsl:when>
                    <xsl:when test="@ItemPosition='2'">
                      <xsl:text>2nd: </xsl:text>
                    </xsl:when>
                    <xsl:when test="@ItemPosition='3'">
                      <xsl:text>3rd: </xsl:text>
                    </xsl:when>
                  </xsl:choose>
                  <xsl:attribute name="dir">
                    <xsl:value-of select="//Settings/Direction"/>
                  </xsl:attribute>
                  <xsl:value-of select="StudyPoint"/>
                </div>

                <xsl:choose>
                  <xsl:when test="Assignment=1">
                    <div class="checkBibleReading"></div>
                  </xsl:when>
                  <xsl:when test="Assignment=2">
                    <div class="checkInitialCall"></div>
                    <div class="fieldInitialCallIndex">
                      <xsl:choose>
                        <xsl:when test="@ItemPosition='1'">
                          <xsl:text>#1</xsl:text>
                        </xsl:when>
                        <xsl:when test="@ItemPosition='2'">
                          <xsl:text>#2</xsl:text>
                        </xsl:when>
                        <xsl:when test="@ItemPosition='3'">
                          <xsl:text>#3</xsl:text>
                        </xsl:when>
                      </xsl:choose>
                    </div>
                  </xsl:when>
                  <xsl:when test="Assignment=3">
                    <div class="checkFirstReturnVisit"></div>
                    <div class="fieldFirstReturnVisitIndex">
                      <xsl:choose>
                        <xsl:when test="@ItemPosition='1'">
                          <xsl:text>#1</xsl:text>
                        </xsl:when>
                        <xsl:when test="@ItemPosition='2'">
                          <xsl:text>#2</xsl:text>
                        </xsl:when>
                        <xsl:when test="@ItemPosition='3'">
                          <xsl:text>#3</xsl:text>
                        </xsl:when>
                      </xsl:choose>
                    </div>
                  </xsl:when>
                  <xsl:when test="Assignment=4">
                    <div class="checkSecondReturnVisit"></div>
                    <div class="fieldSecondReturnVisitIndex">
                      <xsl:choose>
                        <xsl:when test="@ItemPosition='1'">
                          <xsl:text>#1</xsl:text>
                        </xsl:when>
                        <xsl:when test="@ItemPosition='2'">
                          <xsl:text>#2</xsl:text>
                        </xsl:when>
                        <xsl:when test="@ItemPosition='3'">
                          <xsl:text>#3</xsl:text>
                        </xsl:when>
                      </xsl:choose>
                    </div>
                  </xsl:when>
                  <xsl:when test="Assignment=5">
                    <div class="checkThirdReturnVisit"></div>
                  </xsl:when>
                  <xsl:when test="Assignment=6">
                    <div class="checkBibleStudy"></div>
                  </xsl:when>
                  <xsl:when test="Assignment=7">
                    <div class="checkTalk"></div>
                  </xsl:when>
                  <xsl:when test="Assignment=0">
                    <div class="checkOther"></div>
                    <div class="fieldOther">
                      <xsl:attribute name="dir">
                        <xsl:value-of select="//Settings/Direction"/>
                      </xsl:attribute>
                      <xsl:valueof select="Other"/>
                    </div>
                  </xsl:when>
                </xsl:choose>
                <xsl:choose>
                  <xsl:when test="Location=

<details>
<summary>英文:</summary>

Note sure how to make one of your online test environments for this issue.

Test XSL script:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" version="4.01"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
doctype-public="//W3C//DTD XHTML 1.0 Transitional//EN"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<xsl:attribute name="lang">
<xsl:value-of select="//Settings//LanguageCode"/>
</xsl:attribute>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<link rel="stylesheet" type="text/css">
<xsl:attribute name="href">
<xsl:value-of select="//Settings/Template/@Style"/>
</xsl:attribute>
</link>
<title>
<!--<xsl:value-of select="//Labels/ReportTitleWorksheets"/>-->
Assignment Slips
</title>
<style type="text/css">
@import url('<xsl:text>2020/</xsl:text><xsl:value-of select="//Settings/Template/@Style"/>');
</style>
</head>
<body>
<xsl:for-each select="AssignmentSlips/Page">
<div class="containerPage">
<xsl:if test="@PageBreakBefore=1">
<br style="page-break-before: always;"/>
</xsl:if>
<xsl:for-each select="StudentSlip">
<div class="containerSlip">
<img alt="s89" width="323px" height="429px">
<xsl:attribute name="src">
<xsl:text>2020&lt;/xsl:text>
<xsl:value-of select="//Settings/Template"/>
</xsl:attribute>
</img>
<div class="fieldName">
<xsl:attribute name="dir">
<xsl:value-of select="//Settings/Direction"/>
</xsl:attribute>
<xsl:value-of select="Student"/>
</div>
<div class="fieldAssisant">
<xsl:attribute name="dir">
<xsl:value-of select="//Settings/Direction"/>
</xsl:attribute>
<xsl:value-of select="Assistant"/>
</div>
<div class="fieldDate">
<xsl:attribute name="dir">
<xsl:value-of select="//Settings/Direction"/>
</xsl:attribute>
<xsl:value-of select="Date"/>
</div>
<div class="fieldCounsel">
<xsl:choose>
<xsl:when test="@ItemPosition='1' and Assistant!=''">
<xsl:text>1st: </xsl:text>
</xsl:when>
<xsl:when test="@ItemPosition='2'">
<xsl:text>2nd: </xsl:text>
</xsl:when>
<xsl:when test="@ItemPosition='3'">
<xsl:text>3rd: </xsl:text>
</xsl:when>
</xsl:choose>
<xsl:attribute name="dir">
<xsl:value-of select="//Settings/Direction"/>
</xsl:attribute>
<xsl:value-of select="StudyPoint"/>
</div>

            &lt;xsl:choose&gt;
&lt;xsl:when test=&quot;Assignment=1&quot;&gt;
&lt;div class=&quot;checkBibleReading&quot;&gt;✓&lt;/div&gt;
&lt;/xsl:when&gt;
&lt;xsl:when test=&quot;Assignment=2&quot;&gt;
&lt;div class=&quot;checkInitialCall&quot;&gt;✓&lt;/div&gt;
&lt;div class=&quot;fieldInitialCallIndex&quot;&gt;
&lt;xsl:choose&gt;
&lt;xsl:when test=&quot;@ItemPosition=&#39;1&#39;&quot;&gt;
&lt;xsl:text&gt;#1&lt;/xsl:text&gt;
&lt;/xsl:when&gt;
&lt;xsl:when test=&quot;@ItemPosition=&#39;2&#39;&quot;&gt;
&lt;xsl:text&gt;#2&lt;/xsl:text&gt;
&lt;/xsl:when&gt;
&lt;xsl:when test=&quot;@ItemPosition=&#39;3&#39;&quot;&gt;
&lt;xsl:text&gt;#3&lt;/xsl:text&gt;
&lt;/xsl:when&gt;
&lt;/xsl:choose&gt;
&lt;/div&gt;
&lt;/xsl:when&gt;
&lt;xsl:when test=&quot;Assignment=3&quot;&gt;
&lt;div class=&quot;checkFirstReturnVisit&quot;&gt;✓&lt;/div&gt;
&lt;div class=&quot;fieldFirstReturnVisitIndex&quot;&gt;
&lt;xsl:choose&gt;
&lt;xsl:when test=&quot;@ItemPosition=&#39;1&#39;&quot;&gt;
&lt;xsl:text&gt;#1&lt;/xsl:text&gt;
&lt;/xsl:when&gt;
&lt;xsl:when test=&quot;@ItemPosition=&#39;2&#39;&quot;&gt;
&lt;xsl:text&gt;#2&lt;/xsl:text&gt;
&lt;/xsl:when&gt;
&lt;xsl:when test=&quot;@ItemPosition=&#39;3&#39;&quot;&gt;
&lt;xsl:text&gt;#3&lt;/xsl:text&gt;
&lt;/xsl:when&gt;
&lt;/xsl:choose&gt;
&lt;/div&gt;
&lt;/xsl:when&gt;
&lt;xsl:when test=&quot;Assignment=4&quot;&gt;
&lt;div class=&quot;checkSecondReturnVisit&quot;&gt;✓&lt;/div&gt;
&lt;div class=&quot;fieldSecondReturnVisitIndex&quot;&gt;
&lt;xsl:choose&gt;
&lt;xsl:when test=&quot;@ItemPosition=&#39;1&#39;&quot;&gt;
&lt;xsl:text&gt;#1&lt;/xsl:text&gt;
&lt;/xsl:when&gt;
&lt;xsl:when test=&quot;@ItemPosition=&#39;2&#39;&quot;&gt;
&lt;xsl:text&gt;#2&lt;/xsl:text&gt;
&lt;/xsl:when&gt;
&lt;xsl:when test=&quot;@ItemPosition=&#39;3&#39;&quot;&gt;
&lt;xsl:text&gt;#3&lt;/xsl:text&gt;
&lt;/xsl:when&gt;
&lt;/xsl:choose&gt;
&lt;/div&gt;
&lt;/xsl:when&gt;
&lt;xsl:when test=&quot;Assignment=5&quot;&gt;
&lt;div class=&quot;checkThirdReturnVisit&quot;&gt;✓&lt;/div&gt;
&lt;/xsl:when&gt;
&lt;xsl:when test=&quot;Assignment=6&quot;&gt;
&lt;div class=&quot;checkBibleStudy&quot;&gt;✓&lt;/div&gt;
&lt;/xsl:when&gt;
&lt;xsl:when test=&quot;Assignment=7&quot;&gt;
&lt;div class=&quot;checkTalk&quot;&gt;✓&lt;/div&gt;
&lt;/xsl:when&gt;
&lt;xsl:when test=&quot;Assignment=0&quot;&gt;
&lt;div class=&quot;checkOther&quot;&gt;✓&lt;/div&gt;
&lt;div class=&quot;fieldOther&quot;&gt;
&lt;xsl:attribute name=&quot;dir&quot;&gt;
&lt;xsl:value-of select=&quot;//Settings/Direction&quot;/&gt;
&lt;/xsl:attribute&gt;
&lt;xsl:value-of select=&quot;Other&quot;/&gt;
&lt;/div&gt;
&lt;/xsl:when&gt;
&lt;/xsl:choose&gt;
&lt;xsl:choose&gt;
&lt;xsl:when test=&quot;Location=1&quot;&gt;
&lt;div class=&quot;checkMainHall&quot;&gt;✓&lt;/div&gt;
&lt;/xsl:when&gt;
&lt;xsl:when test=&quot;Location=2&quot;&gt;
&lt;div class=&quot;checkAuxClass1&quot;&gt;✓&lt;/div&gt;
&lt;/xsl:when&gt;
&lt;xsl:when test=&quot;Location=3&quot;&gt;
&lt;div class=&quot;checkAuxClass2&quot;&gt;✓&lt;/div&gt;
&lt;/xsl:when&gt;
&lt;/xsl:choose&gt;
&lt;/div&gt;
&lt;/xsl:for-each&gt;
&lt;/div&gt;
&lt;/xsl:for-each&gt;
&lt;/body&gt;
&lt;/html&gt;

</xsl:template>
</xsl:stylesheet>


Test XML data:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="AssignmentSlips-2020-v1.xsl"?>
<AssignmentSlips Version="230800">
<Settings>
<LanguageCode>zu</LanguageCode>
<Template Style="s-89-zul.css">s-89-zul.jpg</Template>
<Direction>ltr</Direction>
<ForeignGroupMode>0</ForeignGroupMode>
</Settings>
<Page PageBreakBefore="0">
<StudentSlip ItemPosition="0">
<Student></Student>
<StudyPoint></StudyPoint>
<Material></Material>
<Assignment ItemCount="1">1</Assignment>
<Location>1</Location>
<Date>ULwesine 9 Februwari 2023</Date>
</StudentSlip>
<StudentSlip ItemPosition="1">
<Student></Student>
<StudyPoint></StudyPoint>
<Assignment ItemCount="1">2</Assignment>
<Location>1</Location>
<Date>ULwesine 9 Februwari 2023</Date>
</StudentSlip>
<StudentSlip ItemPosition="2">
<Student></Student>
<StudyPoint></StudyPoint>
<Assignment ItemCount="1">3</Assignment>
<Location>1</Location>
<Date>ULwesine 9 Februwari 2023</Date>
</StudentSlip>
<StudentSlip ItemPosition="3">
<Student></Student>
<StudyPoint></StudyPoint>
<Material></Material>
<Assignment ItemCount="1">6</Assignment>
<Location>1</Location>
<Date>ULwesine 9 Februwari 2023</Date>
</StudentSlip>
</Page>
</AssignmentSlips>


The referenced CSS / JPG files are in a 2020 sub folder:
https://www.dropbox.com/s/vbbqr10gbgmil8s/2020.zip?dl=0
When I open the XML file with Microsoft Edge the display is blank.
When I did manage to view the source it was cropped:

<!DOCTYPE html PUBLIC "//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="zu" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<link rel="stylesheet" type="text/css" href="s-89-zul.css" />
<title>
Assignment Slips
</title>
<style type="text/css">
@import url('2020/s-89-zul.css');
</style>
</head>
<body>
<div class="containerPage">
<div class="containerSlip">
<img alt="s89" width="323px" height="429px" src="2020\s-89-zul.jpg" />
<div class="fieldName" dir="ltr">
</div>
<div class="fieldAssisant" dir="ltr">
</div>
<div class="fieldDate" dir="ltr">ULwesine 9 Februwari 2023</div>
<div class="fieldCounsel" dir="ltr">
</div>
<div class="checkBibleReading">✓</div>
<div class="checkMainHall">✓</div>
</div>
<div class="containerSlip">
<img alt="s89" width="323px" height="429px" src="2020\s-89-zul.jpg" />
<div class="fieldName" dir="ltr">
</div>
<div class="fieldAssisant" dir="ltr">
</div>
<div class="fieldDate" dir="ltr">ULwesine 9 Februwari 2023</div>
<div class="fieldCounsel" dir="ltr">
</div>
<div class="checkInitialCall">✓</div>
<div class="fieldInitialCallIndex">#1</div>
<div class="checkMainHall">✓</div>
</div>
<div class="containerSlip">
<img alt="s89" width="323px" height="429px" src="2020\s-89-zul.jpg" />
<div class="fieldName" dir="ltr">
</div>
<div class="fieldAssisant" dir="ltr">
</div>
<div class="fieldDate" dir="ltr">ULwesine 9 Februwari 2023</div>
<div class="fieldCounsel">2nd:


What have I done wrong here?
</details>
# 答案1
**得分**: 1
你的第一个问题是在一个无法提供良好诊断信息的环境中测试这段代码。理想情况下,在类似Oxygen的XSLT开发工具中测试你的代码。如果只能在浏览器中测试,使用像Chrome这样带有良好开发者控制台的浏览器。
当我在XSLTfiddle(https://www.xsltfiddle.liberty-development.net)中运行它时,我得到了一个明确的错误信息:
&gt; 在第70行执行XSLT时出错:不能在包含元素的子元素之后创建属性节点(`dir`)。最近的元素开始标签在模块的第58行输出。
如果你看周围的代码:
&lt;div class=&quot;fieldCounsel&quot;&gt;
&lt;xsl:choose&gt;
&lt;xsl:when test=&quot;@ItemPosition=&#39;1&#39; and Assistant!=&#39;&#39;&quot;&gt;
&lt;xsl:text&gt;1st: &lt;/xsl:text&gt;
&lt;/xsl:when&gt;
&lt;xsl:when test=&quot;@ItemPosition=&#39;2&#39;&quot;&gt;
&lt;xsl:text&gt;2nd: &lt;/xsl:text&gt;
&lt;/xsl:when&gt;
&lt;xsl:when test=&quot;@ItemPosition=&#39;3&#39;&quot;&gt;
&lt;xsl:text&gt;3rd: &lt;/xsl:text&gt;
&lt;/xsl:when&gt;
&lt;/xsl:choose&gt;
&lt;xsl:attribute name=&quot;dir&quot;&gt;
&lt;xsl:value-of select=&quot;//Settings/Direction&quot;/&gt;
&lt;/xsl:attribute&gt;
&lt;xsl:value-of select=&quot;StudyPoint&quot;/&gt;
&lt;/div&gt;
很明显,`xsl:choose`正在创建一个文本节点,然后你跟着一个创建属性的指令。你需要在创建子元素之前创建元素的属性。
<details>
<summary>英文:</summary>
Your first problem is that you are testing this code in an environment that gives very poor diagnostics. Ideally, test your code in an XSLT development tool such as Oxygen. If you can only test it in the browser, use a browser like Chrome with a decent developer&#39;s console.
When I run it in XSLTfiddle (https://www.xsltfiddle.liberty-development.net) I get a clear error message:
&gt; Error executing XSLT at line 70 : An attribute node (`dir`) cannot be
&gt; created after a child of the containing element. Most recent element
&gt; start tag was output at line 58 of module
If you look at the surrounding code:
&lt;div class=&quot;fieldCounsel&quot;&gt;
&lt;xsl:choose&gt;
&lt;xsl:when test=&quot;@ItemPosition=&#39;1&#39; and Assistant!=&#39;&#39;&quot;&gt;
&lt;xsl:text&gt;1st: &lt;/xsl:text&gt;
&lt;/xsl:when&gt;
&lt;xsl:when test=&quot;@ItemPosition=&#39;2&#39;&quot;&gt;
&lt;xsl:text&gt;2nd: &lt;/xsl:text&gt;
&lt;/xsl:when&gt;
&lt;xsl:when test=&quot;@ItemPosition=&#39;3&#39;&quot;&gt;
&lt;xsl:text&gt;3rd: &lt;/xsl:text&gt;
&lt;/xsl:when&gt;
&lt;/xsl:choose&gt;
&lt;xsl:attribute name=&quot;dir&quot;&gt;
&lt;xsl:value-of select=&quot;//Settings/Direction&quot;/&gt;
&lt;/xsl:attribute&gt;
&lt;xsl:value-of select=&quot;StudyPoint&quot;/&gt;
&lt;/div&gt;
It&#39;s perfectly clear that the `xsl:choose` is creating a text node, and you then follow this with an instruction that creates an attribute. You need to create the attributes of an element before you create the children.
</details>

huangapple
  • 本文由 发表于 2023年2月8日 21:56:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386827.html
匿名

发表评论

匿名网友

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

确定