使用xslt将atom xml转换为html

编程入门 行业动态 更新时间:2024-10-27 07:21:42
使用xslt将atom xml转换为html - 如何显示原始HTML(Convert atom xml to html with xslt - how to show original HTML)

我正在将原子xml转换为html但在xml中我有显示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>

我不认为HTML里面会显示设计而不是文本

问题在于:

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

I'm converting atom xml to html but in the xml I have element that shows html and in my result I get it as text.

The 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>

The 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>

I wan't that the HTML inside will shown with the design and not as text

The problem is in:

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

最满意答案

你说问题是这个......

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

的确是! 您在此处使用mode属性,但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...

这是因为摘要节点中的元素是原始Feed中“ 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"/>

You say the problem is with this...

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

Indeed it is! You are using the mode attribute here, but there is no template present in your XSLT that has this mode specified. Of course, you might be showing a cut-down version of your XSLT, but assuming not, what will happen in this case is that when XSLT cannot find a template that matches, its built-in templates will be used, and these will ultimately output the text within the nodes.

Try adding this template to your XSLT

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

However, you might find when you do this, the output looks like this...

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

This is because the elements within the summary node are part of the "http://www.w3.org/2005/Atom" namespace in the original feed. If you don't want the namespaces present, try these two templates instead

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

Do note that this line in your XSLT

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

Can be replaced with just this

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

更多推荐

coma,html,p>,电脑培训,计算机培训,IT培训"/> <meta name="description&q

本文发布于:2023-07-18 04:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154951.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   atom   xslt   html   xml

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!