2013-06-28 73 views
2

好的,我有這個文件,我試圖在xsl中進行轉換,以便稍後將其轉換爲rdf數據。這是我迄今爲止在XSL文件由此產生:使用XSLT轉換將XML轉換爲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> 

任何提示將最讚賞,並感謝...

+0

版本=「2.0」:在XSLT 2.0支持在Firefox? – Pierre

+0

這不是所以我已經將它改爲version =「1.0」,現在使用Chrome ... –

+1

如果你想使用XSLT 1.0,那麼你不能使用'' 2.0。您可能需要使用獨立的XSLT 2.0處理器(如Saxon),而不是在瀏覽器中進行轉換。 –

回答

1
<drugs xmlns="http://drugbank.ca" ....> 

這是你的「問題」 - 因爲XML文件有一個默認的命名空間聲明,文件中的所有前綴不變的元素名稱都在這個命名空間中。現在的XPath(1.0)的表達式,前綴的名字總是選擇的節點是在命名空間中,所以

<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> 
+0

你我的先生真的很棒...謝謝 –