1

そのため、さまざまな XML ドキュメントを新しいノード レイアウトに処理するための XSLT ファイルの作成に忙しくしています。

理解できないことが 1 つあります。これは、私が使用している XML の例です。

<page>
   This is a paragraph on the page.
    <newParagraph/>
   This is another paragraph.
    <newParagraph/>
   Here is yet another paragraph on this page.
<page>

ご覧のとおり、段落は空のタグを区切りとして使用して分割されています。結果のXMLでは、これが必要です:

<page>
   <p>
    This is a paragraph on the page.
   </p>
   <p> 
    This is another paragraph.
   </p>
   <p>
   Here is yet another paragraph on this page.
   </p>
<page>

XSLT (バージョン 1.0 のみ) を使用してこれを実現するにはどうすればよいですか?

4

3 に答える 3

0

少し「チート」したい場合は、ノード ツリーの一部ではなく、通常のテキストである XML タグを結果ドキュメントに手動で挿入できます。ただし、下流のプロセッサは、出力を再解析する限り、違いに気付きません。

私の他の回答の入力を考えると、次の XSLT 1.0 変換がうまくいきます (段落内のサブツリーを保持します)。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />

  <xsl:template match="page">
    <page>
      <P>
        <xsl:apply-templates/>
      </P>
    </page>
  </xsl:template>

  <!-- this default rule recursively copies all substructures within a paragraph at tag level -->  
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>


  <!-- this default rule makes sure that texts between the tags are printed -->
  <xsl:template match="text()">
    <xsl:copy-of select="."/>
  </xsl:template>

  <xsl:template match="newParagraph">
    <!-- This inserts a matching closing and opening tag -->
    <xsl:value-of select="'&lt;/P&gt;&lt;P&gt;'" disable-output-escaping="yes" />
  </xsl:template>

</xsl:stylesheet>
于 2014-04-24T06:28:51.733 に答える
0

次の回答は@stwisselほどエレガントではありませんが、段落内のサブツリーを正しくタグ付けします。確かに、それは少し厄介になりました。:-)

このタスクの問題点は、終了タグとそれに続く対応する開始タグ (例: <tag></tag>) の間にあるものを特別に処理する必要があることです。ただし、XSLT は、開始タグとそれに対応する終了タグ (例: ) の間にあるものを処理するために最適化されてい</tag><tag>ます。ところで、少し「ごまかす」方法があります。この質問に対する私の他の回答を参照してください。

次のような入力 XML があるとします。

<pages>
  <page>
    This is a paragraph on the page.
    <B>bold</B>
    After Bold
    <newParagraph/>
    This is another paragraph.
    <newParagraph/>
    Here is yet another paragraph on this page.
    <EM>
      <B>
        Bold and emphasized.
      </B>
    </EM>
    After bold and emphasized.
  </page>
  <page>
    Another page.
  </page>
</pages>

この XSLT 1.0 変換を使用して処理できます

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />

  <xsl:template match="page">
    <page>
      <!-- handle the first paragraph up to the first newParagraph -->
      <P>
        <xsl:apply-templates select="node()[not(preceding-sibling::newParagraph)]" />
      </P>

      <!-- now handle all remaining paragraphs of the page -->
      <xsl:for-each select="newParagraph">
        <xsl:variable name="pCount" select="position()"/>
        <P>
          <xsl:apply-templates select="following-sibling::node()[count(preceding-sibling::newParagraph) &lt;= $pCount]" />
        </P>
      </xsl:for-each>
    </page>
  </xsl:template>

  <!-- this default rule recursively copies all substructures within a paragraph at tag level -->  
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>


  <!-- this default rule makes sure that texts between the tags are printed -->
  <xsl:template match="text()">
    <xsl:copy-of select="."/>
  </xsl:template>

  <xsl:template match="newParagraph"/>

</xsl:stylesheet>

この出力を生成する

<pages>
  <page><P>
    This is a paragraph on the page.
    <B>bold</B>
    After Bold
    </P><P>
    This is another paragraph.
    </P><P>
    Here is yet another paragraph on this page.
    <EM>
      <B>
        Bold and emphasized.
      </B>
    </EM>
    After bold and emphasized.
  </P></page>
  <page><P>
    Another page.
  </P></page>
</pages>
于 2014-04-23T22:26:40.250 に答える