0

我的要求是在页面中显示一些xml元素(它们是 input 的一部分xmlhtml

例如:

<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输出页面中显示,保留输入中跟随的缩进任何人都知道如何使用XSLTor来实现它Xquery

编辑:2012 年 1 月 2 日

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

4

3 回答 3

4

I. 一个简单的方法

这种转变:

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

二、漂亮的、浏览器显示的 XML

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

三、将浏览器所需的所有输入生成为文本( 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>

它由浏览器显示为

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

于 2011-12-28T13:42:17.247 回答
2

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

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>
于 2012-01-02T21:18:26.703 回答
0

以下使用(旧的)FireFox 就像 MarkLogic CQ 中的魅力:

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 标签。我曾经按照 Dimitre 的建议采用“textify”方法(将其放在没有 br 标签的 pre 中)。

于 2012-01-02T18:10:32.310 回答