xml - 添加 XSD 时 XSLT 转换失败

标签 xml xslt xsd

我编写了一个 XML 表,并制作了一个 XSD 来验证它。现在根据this验证器(validome,一如既往地不起作用)模式验证正常,一切都正确。问题是,当我将 xds 实现到 xml 时...表格就消失了。

这是 XML 的实现方式+表格元素示例(波兰语)

<?xml version="1.0" encoding="UTF-8"?>
<!-- DTD <!DOCTYPE gry SYSTEM "valid.dtd">  -->


<?xml-stylesheet type="text/xsl" href="gry.xsl"?>
<gry xmlns="http://www.w3schools.com/RedsDevils"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.w3schools.com/RedsDevils schema.xsd">
  <gatunek id="FPS">
    <gra nazwa="Star Wars: Battlefront" PC="y" XBOXONE="n" PS4="n">
      <producent> DICE </producent>
      <wydawca> Electronic Arts </wydawca>
      <cena> 120 </cena>
      <data_wydania>2015-10-10</data_wydania>
      <metacritic>86</metacritic>
      <cover>img/swb.jpg</cover>
            <opis>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</opis>
    </gra> </gatunek> </gry>

xsd 的情况如何:

    <?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.w3schools.com/RedsDevils"
    elementFormDefault="qualified"
    xmlns="http://www.w3schools.com/RedsDevils"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="gry" >
    <xs:complexType>
      <xs:sequence>
        <xs:element name="gatunek" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="gra" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="producent" type="xs:string"/>
                    <xs:element name="wydawca" type="xs:string"/>
                    <xs:element maxOccurs="1" minOccurs="0" name="cena" type="xs:integer"/>
                    <xs:element maxOccurs="1" minOccurs="0" name="data_wydania" type="xs:date"/>

                    <xs:element maxOccurs="1" minOccurs="0" name="metacritic">
                      <xs:simpleType>
                        <xs:restriction base="xs:byte">
                          <xs:maxInclusive value="100"/>
                          <!-- oceny na metacriticu nie przekraczaja 100 -->
                        </xs:restriction>
                      </xs:simpleType>
                    </xs:element>

                    <xs:element name="cover" type="xs:anyURI"/>

                    <xs:element name="opis">
                      <xs:simpleType>
                        <xs:restriction base="xs:string">
                          <xs:maxLength value="500"/>
                          <xs:minLength value="20"/>
                        </xs:restriction>
                      </xs:simpleType>
                    </xs:element>

                    </xs:sequence>

                    <xs:attribute name="nazwa" type="xs:string" use="required"/>
                    <xs:attribute name="PC" use="required">
                      <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:pattern value="(y|n)"/>
                      </xs:restriction>
                    </xs:simpleType>
                    </xs:attribute>

                    <xs:attribute name="XBOXONE" use="required">
                      <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:pattern value="(y|n)"/>
                      </xs:restriction>
                      </xs:simpleType>
                    </xs:attribute>

                    <xs:attribute name="PS4" use="required">
                      <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:pattern value="(y|n)"/>
                      </xs:restriction>
                      </xs:simpleType>
                    </xs:attribute>


              </xs:complexType>
            </xs:element>
          </xs:sequence>
          <xs:attribute name="id" type="xs:string" use="required"/>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

但是不实现 XSD 效果很好,所以:

<?xml-stylesheet type="text/xsl" href="gry.xsl"?>
<gry>
  <gatunek id="FPS">
    <gra nazwa="Star Wars: Battlefront" PC="y" XBOXONE="n" PS4="n">
      <producent> DICE </producent>
      <wydawca> Electronic Arts </wydawca>(....)

工作并显示一切正常。 firebug 控制台没有显示任何错误。

此外,这里还有 xsl 文件,以备需要时使用:

<?xml version="1.0" encoding="UTF-8"?>

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

  <xsl:output doctype-system="about:legacy-compat" method="html"/>

  <xsl:template match="/">
    <html>
      <HEAD>
        <link href="style.css" rel="stylesheet" type="text/css"/>
        <link href='https://fonts.googleapis.com/css?family=Caudex:400,700' rel='stylesheet' type='text/css'/>
        <script src="js/jquery.js" type="text/javascript"></script>

        <!--BOOTSTRAP!! -->
        <!-- Latest compiled and minified CSS -->
        <link crossorigin="anonymous" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" rel="stylesheet"/>

        <!-- Optional theme -->
        <link crossorigin="anonymous" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" rel="stylesheet"/>

        <!-- Latest compiled and minified JavaScript -->
        <script crossorigin="anonymous" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
        <!-- BOOTSTRAP END -->
        <title>GRY!! :D
        </title>

      </HEAD>
      <BODY>
        <div id="bg"></div>
        <div id="container">
          <h1 id="title">
            GRY 2015 ROKU
          </h1>
          <div id="header">

            <div class="row">
              <div class="col-md-4">
                <div class="menuelm" id="All">
                  <h3>Wszystkie tytuły</h3>
                  <a class="link" href="#">Wyświetl</a>
                </div>
              </div>
              <div class="col-md-4">
                <div class="menuelm" id="Gat">
                  <h3>Filtruj gatunek:</h3>
                  <select>
                    <option>FPS</option>
                    <option>RPG</option>
                    <option>Strategia</option>
                    <option>Bijatyka</option>
                    <option>Horror</option>
                    <option>Wyscigi</option>
                  </select>
                  <a class="gatlink" href="#">Wyświetl</a>
                </div>
              </div>
              <div class="col-md-4">
                <div class="menuelm" id="Sys">
                  <h3>Filtruj Konsole:</h3>
                  <div class="row systems">

                    <a href="#pc"><img alt="pc" src="img/sym/pc.jpg"/></a>
                    <a href="#xbo"><img alt="xbox" src="img/sym/xbox.png"/></a>
                    <a href="#ps"><img alt="ps4" src="img/sym/ps4.jpg"/></a>
                  </div>
                </div>
              </div>
            </div>

          </div>
          <!--HEADER END -->

          <div id="tablecover">

            <xsl:call-template name="all"/>
            <script src="js/gry.js" type="text/javascript"></script>
          </div>
        </div>
        <!--container end!-->
        <div style="clear: both"></div>
        <footer>
          <h3>Projekt Alan Krygowski</h3>
        </footer>
      </BODY>
    </html>
  </xsl:template>

  <xsl:template name="all">
    <table class="tablesorter" id="myTable">
      <thead>
        <tr>
          <td>
            <div>
              <p>Nazwa</p>
            </div>
          </td>
          <td>
            <div>
              <p>Gatunek</p>
            </div>
          </td>
          <td>
            <div>
              <p>Producent</p>
            </div>
          </td>
          <td>
            <div>
              <p>Wydawca</p>
            </div>
          </td>
          <td>
            <div>
              <p>Cena</p>
            </div>
          </td>
          <td>
            <div>
              <p>Data Wydania</p>
            </div>
          </td>
          <td>
            <div>
              <p>Metacritic</p>
            </div>
          </td>
          <td>
            <div>
              <p>PC</p>
            </div>
          </td>
          <td>
            <div>
              <p>XBOX</p>
            </div>
          </td>
          <td>
            <div>
              <p>PS4</p>
            </div>
          </td>
        </tr>
      </thead>
      <tbody>

        <!--  <xsl:for-each select="gatunek/gra[../@id='FPS'] | gatunek/gra[../@id='Strategia']">-->

        <xsl:for-each select="gry/gatunek/gra">
          <tr class="{../@id} ">
            <td>
              <div>
                <p><xsl:value-of select="@nazwa"/></p>
              </div>
            </td>
            <td>
              <div>
                <p><xsl:value-of select="../@id"/></p>
              </div>
            </td>
            <td>
              <div>

                <p><xsl:value-of select="producent"/></p>

              </div>
            </td>
            <td>
              <div>

                <p><xsl:value-of select="wydawca"/></p>

              </div>
            </td>
            <td>
              <div>

                <xsl:choose>
                  <xsl:when test="not(cena)">
                    <p>brak danych</p>
                  </xsl:when>
                  <xsl:otherwise>
                    <p><xsl:value-of select="cena"/>PLN</p>
                  </xsl:otherwise>
                </xsl:choose>

              </div>
            </td>
            <td>
              <div>

                <xsl:choose>
                  <xsl:when test="not(data_wydania)">
                    <p>brak danych</p>
                  </xsl:when>
                  <xsl:otherwise>
                    <p><xsl:value-of select="data_wydania"/></p>
                  </xsl:otherwise>
                </xsl:choose>


              </div>
            </td>
            <td>
              <div>

                <xsl:choose>
                  <xsl:when test="not(metacritic)">
                    <p>brak danych</p>
                  </xsl:when>
                  <xsl:otherwise>
                    <p><xsl:value-of select="metacritic"/>/100</p>
                  </xsl:otherwise>
                </xsl:choose>


              </div>
            </td>

            <!-- OD AUTORA
                Wczesniej, do tych trzech punktow, xsl zawieral zapytanie if odpowiednio
                dostosowujace rodzaj klasy ktora miala byc wstawiona. Zostalo to zamienione
                poniewaz przez dopisywanie klasy zmniejszamy niepotrzebne zablocenie strony -->
            <td>
              <div class="pc {@PC}"></div>
            </td>
            <td>
              <div class="xbo {@XBOXONE}"></div>
            </td>
            <td>
              <div class="ps {@PS4}"></div>
            </td>
          </tr>
          <div class="underbox">
            <img alt="coverart">

              <xsl:attribute name="src">
                <xsl:value-of select="cover"/>
              </xsl:attribute>
            </img>
            <div class="about">
              <p><xsl:value-of select="opis"/></p>
            </div>
          </div>

        </xsl:for-each>
      </tbody>
    </table>

  </xsl:template>

</xsl:stylesheet>

此问题背后的原因可能是什么?如何解决?

最佳答案

问题不在于 XSD 的实现,而在于 XML 中引入的命名空间,而 XSLT 中没有考虑到这一点。

添加

xmlns:rd="http://www.w3schools.com/RedsDevils"

添加到 XSLT 中的 xsl:stylesheet 元素,然后在您希望匹配 XML 元素的位置使用 rd 命名空间前缀,这样您的表格就可以工作了将包含 xsi:schemaLocation 的 XML 添加到您的 XSD。

修复了 XSLT 样式表

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rd="http://www.w3schools.com/RedsDevils">

  <xsl:output doctype-system="about:legacy-compat" method="html"/>

  <xsl:template match="/">
    <html>
      <HEAD>
        <link href="style.css" rel="stylesheet" type="text/css"/>
        <link href='https://fonts.googleapis.com/css?family=Caudex:400,700' rel='stylesheet' type='text/css'/>
        <script src="js/jquery.js" type="text/javascript"></script>

        <!--BOOTSTRAP!! -->
        <!-- Latest compiled and minified CSS -->
        <link crossorigin="anonymous" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" rel="stylesheet"/>

        <!-- Optional theme -->
        <link crossorigin="anonymous" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" rel="stylesheet"/>

        <!-- Latest compiled and minified JavaScript -->
        <script crossorigin="anonymous" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
        <!-- BOOTSTRAP END -->
        <title>GRY!! :D
        </title>

      </HEAD>
      <BODY>
        <div id="bg"></div>
        <div id="container">
          <h1 id="title">
            GRY 2015 ROKU
          </h1>
          <div id="header">

            <div class="row">
              <div class="col-md-4">
                <div class="menuelm" id="All">
                  <h3>Wszystkie tytuły</h3>
                  <a class="link" href="#">Wyświetl</a>
                </div>
              </div>
              <div class="col-md-4">
                <div class="menuelm" id="Gat">
                  <h3>Filtruj gatunek:</h3>
                  <select>
                    <option>FPS</option>
                    <option>RPG</option>
                    <option>Strategia</option>
                    <option>Bijatyka</option>
                    <option>Horror</option>
                    <option>Wyscigi</option>
                  </select>
                  <a class="gatlink" href="#">Wyświetl</a>
                </div>
              </div>
              <div class="col-md-4">
                <div class="menuelm" id="Sys">
                  <h3>Filtruj Konsole:</h3>
                  <div class="row systems">

                    <a href="#pc"><img alt="pc" src="img/sym/pc.jpg"/></a>
                    <a href="#xbo"><img alt="xbox" src="img/sym/xbox.png"/></a>
                    <a href="#ps"><img alt="ps4" src="img/sym/ps4.jpg"/></a>
                  </div>
                </div>
              </div>
            </div>

          </div>
          <!--HEADER END -->

          <div id="tablecover">

            <xsl:call-template name="all"/>
            <script src="js/gry.js" type="text/javascript"></script>
          </div>
        </div>
        <!--container end!-->
        <div style="clear: both"></div>
        <footer>
          <h3>Projekt Alan Krygowski</h3>
        </footer>
      </BODY>
    </html>
  </xsl:template>

  <xsl:template name="all">
    <table class="tablesorter" id="myTable">
      <thead>
        <tr>
          <td>
            <div>
              <p>Nazwa</p>
            </div>
          </td>
          <td>
            <div>
              <p>Gatunek</p>
            </div>
          </td>
          <td>
            <div>
              <p>Producent</p>
            </div>
          </td>
          <td>
            <div>
              <p>Wydawca</p>
            </div>
          </td>
          <td>
            <div>
              <p>Cena</p>
            </div>
          </td>
          <td>
            <div>
              <p>Data Wydania</p>
            </div>
          </td>
          <td>
            <div>
              <p>Metacritic</p>
            </div>
          </td>
          <td>
            <div>
              <p>PC</p>
            </div>
          </td>
          <td>
            <div>
              <p>XBOX</p>
            </div>
          </td>
          <td>
            <div>
              <p>PS4</p>
            </div>
          </td>
        </tr>
      </thead>
      <tbody>

        <!--  <xsl:for-each select="gatunek/gra[../@id='FPS'] | gatunek/gra[../@id='Strategia']">-->

        <xsl:for-each select="rd:gry/rd:gatunek/rd:gra">
          <tr class="{../@id} ">
            <td>
              <div>
                <p><xsl:value-of select="@nazwa"/></p>
              </div>
            </td>
            <td>
              <div>
                <p><xsl:value-of select="../@id"/></p>
              </div>
            </td>
            <td>
              <div>

                <p><xsl:value-of select="rd:producent"/></p>

              </div>
            </td>
            <td>
              <div>

                <p><xsl:value-of select="rd:wydawca"/></p>

              </div>
            </td>
            <td>
              <div>

                <xsl:choose>
                  <xsl:when test="not(rd:cena)">
                    <p>brak danych</p>
                  </xsl:when>
                  <xsl:otherwise>
                    <p><xsl:value-of select="rd:cena"/>PLN</p>
                  </xsl:otherwise>
                </xsl:choose>

              </div>
            </td>
            <td>
              <div>

                <xsl:choose>
                  <xsl:when test="not(rd:data_wydania)">
                    <p>brak danych</p>
                  </xsl:when>
                  <xsl:otherwise>
                    <p><xsl:value-of select="rd:data_wydania"/></p>
                  </xsl:otherwise>
                </xsl:choose>


              </div>
            </td>
            <td>
              <div>

                <xsl:choose>
                  <xsl:when test="not(rd:metacritic)">
                    <p>brak danych</p>
                  </xsl:when>
                  <xsl:otherwise>
                    <p><xsl:value-of select="rd:metacritic"/>/100</p>
                  </xsl:otherwise>
                </xsl:choose>


              </div>
            </td>

            <!-- OD AUTORA
                Wczesniej, do tych trzech punktow, xsl zawieral zapytanie if odpowiednio
                dostosowujace rodzaj klasy ktora miala byc wstawiona. Zostalo to zamienione
                poniewaz przez dopisywanie klasy zmniejszamy niepotrzebne zablocenie strony -->
            <td>
              <div class="pc {@PC}"></div>
            </td>
            <td>
              <div class="xbo {@XBOXONE}"></div>
            </td>
            <td>
              <div class="ps {@PS4}"></div>
            </td>
          </tr>
          <div class="underbox">
            <img alt="coverart">

              <xsl:attribute name="src">
                <xsl:value-of select="rd:cover"/>
              </xsl:attribute>
            </img>
            <div class="about">
              <p><xsl:value-of select="rd:opis"/></p>
            </div>
          </div>

        </xsl:for-each>
      </tbody>
    </table>

  </xsl:template>

</xsl:stylesheet>

关于xml - 添加 XSD 时 XSLT 转换失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34839055/

相关文章:

java - 使用 JAXB 和 xjc maven 插件生成 Java 类

java - 什么 xmlAdapter 使用 JAXB 来解码 base64Binary

xslt - xsl 文本转换中的空格

xml - XML 模式如何要求元素按属性排序?

python - 在 xpath 中使用破折号在 py-dom-xpath 中不起作用

java - 如何在每次使用 Java 运行程序时创建一个新文件

java - 升级到 java7u25 后 XML dig sig 错误

android - Eclipse 不显示 XML 错误

javascript - 获取 XSLTProcessor 的错误

java - 使用 Java 的 XSLT