显示自我关闭xml元素到html输出

时间:2011-12-28 11:39:25

标签: html xml xslt xquery marklogic

我的要求是在xml页面中显示一些xml元素(这是输入html的一部分)。

例如:

<org:specs>
  <org:wild animal="6" species="land"/>
  <org:fish animal="7" species="water"/>
  <org:bird animal="8" species="trees"/>
  <org:mammal animal="9" species="land"/>
</org:specs>

我希望整个xml代码段显示在html输出页面中,保留输入后面的缩进 任何人都知道如何使用XSLTXquery

来实现它

编辑:2012年1月2日

我尝试了第一个解决方案(如下所示)。它有效,但我丢失了缩进。 提供更多详细信息,我的案例中的实现将使用Xquery。我将使用Marklogic命令xdmp:xslt-eval(<Stylesheet>,<A-Xquery-function-retrieving-the-above-xml>)。当我使用第一个解决方案时,生成的html页面没有缩进

3 个答案:

答案 0 :(得分:4)

<强>予。一个简单的方法:

这种转变:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:org="some:org" exclude-result-prefixes="org">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="org:specs">
     <xmp>
       <xsl:copy-of select="."/>
     </xmp>
 </xsl:template>
</xsl:stylesheet>

应用于此XML文档(提供的格式不正确的文本构成格式良好的XML文档):

<t xmlns:org="some:org">
<org:specs>
  <org:wild animal="6" species="land"/>
  <org:fish animal="7" species="water"/>
  <org:bird animal="8" species="trees"/>
  <org:mammal animal="9" species="land"/>
</org:specs>
</t>

<强>产生

<xmp>
   <org:specs xmlns:org="some:org">
      <org:wild animal="6" species="land"/>
      <org:fish animal="7" species="water"/>
      <org:bird animal="8" species="trees"/>
      <org:mammal animal="9" species="land"/>
   </org:specs>
</xmp>

,这会以所需方式显示在浏览器中

<org:specs xmlns:org="some:org">
  <org:wild animal="6" species="land"/>
  <org:fish animal="7" species="water"/>
  <org:bird animal="8" species="trees"/>
  <org:mammal animal="9" species="land"/>
</org:specs>

<强> II。看起来很漂亮,浏览器显示的XML

请参阅 XPath Visualizer 的XSLT代码,了解此应用程序如何产生此类结果。

<强> III。以文本(method="text")生成浏览器的所有必需输入:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text" encoding="utf-8"/>

 <xsl:template match="/*">
  <xsl:apply-templates select="*[1]" mode="textify"/>
 </xsl:template>

 <xsl:template match="*/*[*]" mode="textify">
         <xsl:text>&amp;lt;</xsl:text>
          <xsl:value-of select="name()"/>

   <xsl:apply-templates select="@*" mode="textify"/>
   <xsl:text>></xsl:text>
   <xsl:apply-templates select="*|text()" mode="textify"/>

         <xsl:text>&amp;lt;/</xsl:text>
          <xsl:value-of select="name()"/>
   <xsl:text>></xsl:text>
 </xsl:template>

 <xsl:template match="*/*[not(node())]" mode="textify">
         <xsl:text>&amp;lt;</xsl:text>
          <xsl:value-of select="name()"/>

   <xsl:apply-templates select="@*" mode="textify"/>
   <xsl:text>/></xsl:text>
 </xsl:template>

 <xsl:template match="@*" mode="textify">
  <xsl:text> </xsl:text>
  <xsl:value-of select="name()"/>
  <xsl:text>="</xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>"</xsl:text>
 </xsl:template>

 <xsl:template match="text()" mode="textify">
  <xsl:call-template name="textify">
   <xsl:with-param name="pText" select="."/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template name="textify">
  <xsl:param name="pText"/>

  <xsl:if test="string-length($pText) >0">
   <xsl:variable name="vChar" select="substring($pText,1,1)"/>
    <xsl:choose>
     <xsl:when test="$vChar = ' '">
       <xsl:value-of select="'&amp;#xA0;'"/>
     </xsl:when>
     <xsl:when test="$vChar = '&#9;'">
       <xsl:value-of select="'&amp;#xA0;&amp;#xA0;'"/>
     </xsl:when>
     <xsl:when test="$vChar = '&#xA;'">
       <xsl:value-of select="'&lt;br />'"/>
     </xsl:when>
     <xsl:otherwise>
      <xsl:value-of select="$vChar"/>
     </xsl:otherwise>
    </xsl:choose>
  <xsl:call-template name="textify">
   <xsl:with-param name="pText" select=
    "substring($pText, 2)"/>
  </xsl:call-template>
  </xsl:if>
 </xsl:template>

</xsl:stylesheet>

当此转换应用于同一XML文档(上图)时,会生成所需结果

&lt;org:specs><br />&#xA0;&#xA0;&lt;org:wild animal="6" species="land"/><br />&#xA0;&#xA0;&lt;org:fish animal="7" species="water"/><br />&#xA0;&#xA0;&lt;org:bird animal="8" species="trees"/><br />&#xA0;&#xA0;&lt;org:mammal animal="9" species="land"/><br />&lt;/org:specs>

,浏览器将其显示为

&lt; org:specs&gt;
&lt; org:wild animal =“6”species =“land”/&gt;
&lt; org:fish animal =“7”species =“water” /&gt;
&lt; org:bird animal =“8”species =“trees”/&gt;
&lt; org:哺乳动物=“9”种=“土地”/&gt;
&LT; /组织:规格&GT;

答案 1 :(得分:2)

根据浏览器的不同,HTML不会很好地保留空白区域,除非您开始使用CSS,这在浏览器中不是统一支持的。我执行以下操作,在显示和字符序列化中保留空白区域:

xquery version "1.0-ml";

declare namespace org = "someorg";

let $xml := 
<org:specs>
  <org:wild animal="6" species="land"/>
  <org:fish animal="7" species="water"/>
  <org:bird animal="8" species="trees"/>
  <org:mammal animal="9" species="land"/>
</org:specs>


let $quote-options :=
<options xmlns="xdmp:quote">
  <method>xml</method>
  <indent>yes</indent>
  <indent-untyped>yes</indent-untyped>
</options>

return

<div><pre>
{xdmp:quote($xml, $quote-options)}
</pre></div>

答案 2 :(得分:0)

下面的工作就像MarkLogic CQ中的一个魅力,使用(旧的)FireFox:

let $xml :=
<org:specs xmlns:org="some:org"> 
  <org:wild animal="6" species="land"/> 
  <org:fish animal="7" species="water"/> 
  <org:bird animal="8" species="trees"/> 
  <org:mammal animal="9" species="land"/> 
</org:specs> 

return

<html><body>
<h1>Org</h1>

<xmp>{$xml}</xmp>

</body>
</html>

虽然不确定所有浏览器都支持xmp标记。我曾经采用'textify'方法(将它放在没有br标签的pre中),如Dimitre所建议。