【问题标题】:Converting XML to XSL With XSLT Transformations使用 XSLT 转换将 XML 转换为 XSL
【发布时间】:2013-06-28 14:37:03
【问题描述】:

好的,我有这个文件,我正试图在 xsl 中转换,以便以后可以将其转换为 rdf 数据。这是我迄今为止在 xsl 文件中创建的内容:

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

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:template match="/">
<html>
<head>
        <h1>DrugBank Data</h1>
</head>
<body>
    <table border ="2">
    <thead>
        <tr>
            <th>Drugbank Id</th>
            <th>Name</th>
            <th>Description</th>
            <th>Substrate</th>
            <th>Enzymes</th>
            <th>Mechanism Of Action</th>
            <th>Targets</th>
        </tr>
    </thead>
    <tbody>
        <xsl:for-each select="drugs/drug">
        <tr>
            <td><xsl:value-of select="drugbank-id"/></td>
                <td><xsl:value-of select="name"/></td>
            <td><xsl:value-of select="description"/></td>
            <td><xsl:value-of select="substrate"/></td>
            <td><xsl:value-of select="enzymes"/></td>
            <td><xsl:value-of select="mechanism-of-action"/></td>
            <td><xsl:value-of select="targets"/></td>
        </tr>
        </xsl:for-each>
    </tbody>
    </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

这是我的 xml 文件的示例:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet  type="text/xsl" version="2.0" href="drugbank.xsl"?>

<drugs xmlns="http://drugbank.ca" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" schemaVersion="1.4" xs:schemaLocation="http://www.drugbank.ca/docs/drugbank.xsd">
  <drug type="biotech" created="2005-06-13 07:24:05 -0600" updated="2013-05-12 21:37:25 -0600" version="3.0">
    <drugbank-id>DB00001</drugbank-id>
    <name>Lepirudin</name>
    <description>Lepirudin is identical to natural hirudin except for substitution of leucine for isoleucine at the N-terminal end of the molecule and the absence of a sulfate group on the tyrosine at position 63. It is produced via yeast cells.&#xD;</description>
    <cas-number>120993-53-5</cas-number>
    <synthesis-reference></synthesis-reference>
    <indication>For the treatment of heparin-induced thrombocytopenia</indication>
    <pharmacology>Lepirudin is used to break up clots and to reduce thrombocytopenia. It binds to thrombin and prevents thrombus or clot formation. It is a highly potent, selective, and essentially irreversible inhibitor of thrombin and clot-bond thrombin. Lepirudin requires no cofactor for its anticoagulant action. Lepirudin is a recombinant form of hirudin, an endogenous anticoagulant found in medicinal leeches.</pharmacology>
    <mechanism-of-action>Lepirudin forms a stable non-covalent complex with alpha-thrombin, thereby abolishing its ability to cleave fibrinogen and initiate the clotting cascade. The inhibition of thrombin prevents the blood clotting cascade. </mechanism-of-action>

任何提示将不胜感激,谢谢...

【问题讨论】:

  • version="2.0" : Firefox 支持 XSLT 2.0 吗?
  • 不是这样我把它改成 version="1.0" 现在使用 Chrome...
  • 如果您想使用 XSLT 1.0,那么您不能使用 &lt;xsl:result-document&gt;,因为它是在 2.0 中引入的。您可能需要使用独立的 XSLT 2.0 处理器,例如 Saxon,而不是在浏览器中进行转换。
  • 根据我的经验,浏览器不能很好地处理 xslt。获得一个真正为您进行转换的程序可能会更好。最坏的情况是,您始终可以使用 Java 为您编译和转换您的 xsl。
  • 我只是想知道我是否正在盯着我的 xsl 文件。我认为这是我的问题...

标签: xml xslt


【解决方案1】:
<drugs xmlns="http://drugbank.ca" ....>

这是你的“问题”——因为 XML 文件有一个默认的命名空间声明,所以文件中所有不带前缀的元素名称都在这个命名空间中。现在在 XPath (1.0) 表达式中,无前缀名称 always 选择名称空间中 not 的节点,所以

<xsl:for-each select="drugs/drug">

什么都不选择。您需要在样式表中将前缀绑定到 http://drugbank.ca 命名空间,并在 XPath 表达式中使用此前缀

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:db="http://drugbank.ca"
    exclude-result-prefixes="db">

<xsl:output method="html" encoding="UTF-8"/>
<xsl:template match="/">
    <!-- ... boilerplate omitted ... -->
    <tbody>
        <xsl:for-each select="db:drugs/db:drug">
        <tr>
            <td><xsl:value-of select="db:drugbank-id"/></td>
            <td><xsl:value-of select="db:name"/></td>
            <td><xsl:value-of select="db:description"/></td>
            <td><xsl:value-of select="db:substrate"/></td>
            <td><xsl:value-of select="db:enzymes"/></td>
            <td><xsl:value-of select="db:mechanism-of-action"/></td>
            <td><xsl:value-of select="db:targets"/></td>
        </tr>
        </xsl:for-each>
    </tbody>

【讨论】:

    猜你喜欢
    • 2021-01-10
    • 2012-02-01
    • 2019-02-02
    • 2016-08-24
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 2019-03-22
    • 2022-01-07
    相关资源
    最近更新 更多