使用两个名称空间将XSL转换为HTML

时间:2014-09-08 08:19:30

标签: xml xslt

我有xml文件,它为游戏和dlc使用两个命名空间。我现在正在尝试将简单的xls转换为html作为表格。但我无法让它工作,我已经解决了问题,它必须是名称空间。 html总是空白的,只有表头。我100%确定模式是否有效,因为xml验证不会产生任何错误。

xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<GameStore xmlns="http://www.steamkauppa.fi/game"
           xmlns:d="http://www.steamkauppa.fi/dlc"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           
           xsi:schemaLocation="http://www.steamkauppa.fi/game Steam-kauppa.xsd http://www.steamkauppa.fi/dlc SteamDLC.xsd">
    <Game>
        <Name>Borderlands 2</Name>  
        <Category>Action Games</Category>
        <ReleaseDate>21 Sep 2012</ReleaseDate>
        <Developer>Gearbox Software</Developer>
        <AgeLimit>18</AgeLimit>
        <Description>A new era of shoot and loot is about to begin. Play as one 
            of four new vault hunters facing off against a massive new world of 
            creatures, psychos and the evil mastermind, Handsome Jack. Make new 
            friends, arm them with a bazillion weapons and fight alongside them 
            in 4 player co-op on a relentless quest for revenge and redemption 
            across the undiscovered and unpredictable living planet.</Description>
        <Price currency = "EUR">29.99</Price>
        <DiscountPercent>75</DiscountPercent>
        <DLCPacks>
            <DLC>
                <d:Name>Borderlands 2 - Psycho Pack</d:Name>
                <d:Price currency = "EUR">9.99</d:Price>
                <d:DiscountPercent>67</d:DiscountPercent>
            </DLC>    
            <DLC>
                <d:Name>Borderlands 2: Ultimate Vault Hunter Upgrade Pack 2</d:Name>
                <d:Price currency = "EUR">3.99</d:Price>
                <d:DiscountPercent>67</d:DiscountPercent>
            </DLC>      
        </DLCPacks>
    </Game>

    <Game>
        <Name>7 Days to Die</Name>  
        <Category>Action Games</Category>
        <ReleaseDate>13 Dec 2013</ReleaseDate>
        <Developer>The Fun Pimps</Developer>
        <AgeLimit>18</AgeLimit>
        <Description>Building on survivalist and horror themes, players in 7 
            Days to Die can scavenge the abandoned cities of the buildable and 
            destructable voxel world for supplies or explore the wilderness to 
            gather raw materials to build their own tools, weapons, traps, 
            fortifications and shelters. In coming updates these features will 
            be expanded upon with even more depth and a wider variety of choices
            to survive the increasing dangers of the world. Play alone or with 
            friends, run your own server or join others.</Description>
        <Price currency = "EUR">22.99</Price>    
        <DiscountAmount>12</DiscountAmount>   
    </Game>       
</GameStore>

架构文件1:

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->

<xsd:schema version="1.0"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.steamkauppa.fi/game"
            xmlns="http://www.steamkauppa.fi/game"
            xmlns:d="http://www.steamkauppa.fi/dlc"
            elementFormDefault="qualified">

      <xsd:import namespace="http://www.steamkauppa.fi/dlc" 
       schemaLocation="SteamDLC.xsd"/>

    <xsd:element name="Price">
        <xsd:complexType>
            <xsd:simpleContent>
                <xsd:extension base="xsd:double">
                    <xsd:attribute name="currency" type="xsd:string" use="required"/>
                </xsd:extension>
            </xsd:simpleContent>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="Name" type="xsd:string"/>
    <xsd:element name="Category" type="xsd:string"/>
    <xsd:element name="ReleaseDate" type="xsd:string"/>
    <xsd:element name="Developer" type="xsd:string"/>
    <xsd:element name="AgeLimit" type="xsd:int"/>
    <xsd:element name="DiscountPercent" type="xsd:int"/>
    <xsd:element name="DiscountAmount" type="xsd:int"/>
    <xsd:element name="Description" type="xsd:string"/>

    <xsd:complexType name="gameType">
        <xsd:sequence>
            <xsd:element ref="Name"/>
            <xsd:element ref="Category"/>
            <xsd:element ref="ReleaseDate"/>
            <xsd:element ref="Developer"/>
            <xsd:element ref="AgeLimit"/>
            <xsd:element ref="Description"/>  
            <xsd:element ref="Price"/>  
            <xsd:element ref="DiscountPercent" minOccurs="0" maxOccurs="1"/>
            <xsd:element ref="DiscountAmount" minOccurs="0" maxOccurs="1"/>  
            <xsd:element ref="DLCPacks" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>


    <xsd:element name="GameStore">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Game" type="gameType" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="DLCPacks">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="DLC" type="d:dlcType" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>




</xsd:schema>

架构文件2:

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->

<xsd:schema version="1.0"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.steamkauppa.fi/dlc"
            xmlns="http://www.steamkauppa.fi/dlc" elementFormDefault="qualified">

    <xsd:element name="Price">
        <xsd:complexType>
            <xsd:simpleContent>
                <xsd:extension base="xsd:double">
                    <xsd:attribute name="currency" type="xsd:string" use="required"/>
                </xsd:extension>
            </xsd:simpleContent>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="Name" type="xsd:string"/>
    <xsd:element name="DiscountPercent" type="xsd:int"/>
    <xsd:element name="DiscountAmount" type="xsd:int"/>


    <xsd:complexType name="dlcType">
        <xsd:sequence>
            <xsd:element ref="Name"/> 
            <xsd:element ref="Price"/>  
            <xsd:element ref="DiscountPercent" minOccurs="0" maxOccurs="1"/>   
            <xsd:element ref="DiscountAmount" minOccurs="0" maxOccurs="1"/>  
        </xsd:sequence>
    </xsd:complexType>



</xsd:schema>

xsl文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>

<xsl:template match="/">
  <html>
  <body>
  <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th style="text-align:left">Name</th>
        <th style="text-align:left">Category</th>
      </tr>
      <xsl:for-each select="GameStore/Game">
      <tr>
        <td><xsl:value-of select="Name"/></td>
        <td><xsl:value-of select="Category"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

阅读有关XML命名空间的一些教程,已经在stackoverflow.com上询问并回答了很多。

在XML输入中有一个声明为xmlns="http://www.steamkauppa.fi/game"的默认命名空间,这意味着输入中所有未加前缀的元素都属于该命名空间。

为了能够对这些元素进行XSLT转换,将命名空间添加到XSLT中:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:game="http://www.steamkauppa.fi/game" exclude-result-prefixes="game">

既然在你的XSLT中声明了名称空间,你应该使用它,参见下一个调整:

<xsl:for-each select="game:GameStore/game:Game">
<xsl:value-of select="game:Name"/>

完全调整的XSLT:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:game="http://www.steamkauppa.fi/game" exclude-result-prefixes="game">

<xsl:template match="/">
  <html>
  <body>
  <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th style="text-align:left">Name</th>
        <th style="text-align:left">Category</th>
      </tr>
      <xsl:for-each select="game:GameStore/game:Game">
      <tr>
        <td><xsl:value-of select="game:Name"/></td>
        <td><xsl:value-of select="game:Category"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>