0

私の要件は、ページにいくつかの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出力ページにそのまま表示し、入力に続くインデントを保持したいのですが、XSLTまたは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>

II。見栄えの良い、ブラウザで表示されるXML

このアプリケーションがこのような結果を生成する方法については、 XPathVisualizerの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>

この変換を同じXM​​Lドキュメント(上記)に適用すると、目的の結果が生成されます

&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を使用したMarkLogicCQの魅力のように機能します。

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 に答える