2014-01-29 53 views
0

我將atom xml轉換爲html,但是在xml中我有元素,它顯示html,並在我的結果中將其作爲文本獲取。使用xslt將原子xml轉換爲html - 如何顯示原始HTML

的XML:

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:v3="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xml:base="http://apps.nlm.nih.gov/medlineplus/services/" xml:lang="es"> 
<title type="text">MedlinePlus Connect</title> 
<subtitle type="text">MedlinePlus Connect results for ICD-9-CM 250.33</subtitle> 
<author> 
    <name>U.S. National Library of Medicine</name> 
    <uri>http://www.nlm.nih.gov</uri> 
</author> 
<updated type="text">2014-01-29T01:01:09Z</updated> 
<category scheme="REDS_MT010001UV" term="MATCHED"> 
    <v3:mainSearchCriteria xmlns:v3="urn:hl7-org:v3" classCode="OBS" moodCode="DEF"> 
     <v3:code xmlns:v3="urn:hl7-org:v3" code="KSUBJ" codeSystem="2.16.840.1.113883.5.4"/> 
     <v3:value xmlns:v3="urn:hl7-org:v3" code="250.33" codeSystem="2.16.840.1.113883.6.103" displayName="Diabetes mellitus with other coma type 1 uncontrolled"/> 
    </v3:mainSearchCriteria> 
    <v3:informationRecipient xmlns:v3="urn:hl7-org:v3" typeCode="IRCP"> 
     <v3:patient xmlns:v3="urn:hl7-org:v3" classCode="PAT"/> 
    </v3:informationRecipient> 
</category> 
<id/> 
<entry> 
    <title>Coma</title> 
    <link href="http://www.nlm.nih.gov/medlineplus/spanish/coma.html" rel="alternate"/> 
    <id> tag: nlm.nih.gov, 2014-29-01:/medlineplus/spanish/coma.html </id> 
    <updated>2014-01-29T01:01:09Z</updated> 
    <summary type="html"> 
     <p class="NLMalsoCalled">Otros nombres: Estado vegetativo</p> <p>El coma es un estado profundo de inconsciencia. Una persona en coma está viva pero incapaz de moverse o responder a su entorno. El estado de coma se puede presentar como una complicación de una enfermedad subyacente o como resultado de lesiones, tales como <a href="http://www.nlm.nih.gov/medlineplus/spanish/traumaticbraininjury.html">traumatismo del cráneo</a>. </p> 
     <p>El estado de coma rara vez dura más de 2 a 4 semanas. El resultado depende de la causa, la severidad y sitio de la lesión. La gente puede salir de un coma con problemas físicos, intelectuales y psicológicos. Algunas personas pueden permanecer en coma durante años o incluso décadas. Para esa gente, la causa de muerte más común es una infección, como una neumonía. </p> 
     <p class="NLMattribution"> NIH: Instituto Nacional de Trastornos Neurológicos y Accidentes Cerebrovasculares</p> <p class="NLMrelatedLinks"><ul><li><a href="http://www.nlm.nih.gov/medlineplus/spanish/ency/article/003931.htm">Electroencefalograma</a></li></ul></p> 
    </summary> 
</entry> 
</feed> 

的XSLT:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" 
xmlns:atom="http://www.w3.org/2005/Atom" 
version="1.0"> 
<xsl:output method="html"/> 
    <xsl:template match="atom:feed"> 
    <html> 
     <body> 
      <atom:Feed> 
        <atom:entries rdf:parseType="Collection"> 
        <xsl:for-each select="atom:entry"> 
         <atom:Entry> 
          <p> 
          <xsl:for-each select="atom:title"> 
            <xsl:apply-templates select="." mode="object"/> 
          </xsl:for-each> 
          </p> 
          <p> 
          <a href="{atom:link/@href}">For More Data</a> 
          </p> 
          <p> 
          <xsl:for-each  select="atom:summary"> 
            <xsl:apply-templates select="." mode="html"/> 
          </xsl:for-each> 
          </p> 
        </atom:Entry> 
       </xsl:for-each> 
       </atom:entries> 
     </atom:Feed> 
    </body> 
</html> 
    </xsl:template> 
</xsl:stylesheet> 

我wan't這裏面 的HTML將顯示的設計,而不是作爲文本

的問題是在:

 <xsl:for-each select="atom:summary"> 
           <xsl:apply-templates select="." mode="html"/> 
        </xsl:for-each> 

回答

1

你說的問題是這...

<xsl:apply-templates select="." mode="html"/> 

確實是這樣!您在此處使用模式屬性,但在指定此模式的XSLT中不存在任何模板。當然,您可能會展示XSLT的精簡版本,但假設沒有,在這種情況下會發生什麼情況是,當XSLT找不到匹配的模板時,將使用其內置模板,並且這些模板將最終輸出節點內的文本。

嘗試添加該模板應用到XSLT

<xsl:template match="*" mode="html"> 
    <xsl:copy-of select="." /> 
</xsl:template> 

然而,你可能會發現,當你這樣做時,輸出看起來像這樣...

<p xmlns="http://www.w3.org/2005/Atom" class="NLMalsoCalled">Otros nombres... 

這是因爲內的元素摘要節點是原始提要中「http://www.w3.org/2005/Atom」名稱空間的一部分。如果你不希望目前的命名空間,請嘗試以下兩個模板代替

<xsl:template match="*" mode="html"> 
    <xsl:element name="{local-name()}"> 
     <xsl:apply-templates select="@*|node()" mode="html"/> 
    </xsl:element> 
    </xsl:template> 

    <xsl:template match="@*" mode="html"> 
    <xsl:copy-of select="." /> 
    </xsl:template> 

請注意,這條線在你的XSLT

<xsl:for-each  select="atom:summary"> 
    <xsl:apply-templates select="." mode="html"/> 
</xsl:for-each> 

可以只用這

<xsl:apply-templates select="atom:summary" mode="html"/> 
+0

將被替換第一個解決方案 - 謝謝! – user3249888