2

前回の投稿からのフィードバックは魅力的でしたが、システム上のいくつかのドキュメントで次のような問題が発生しました。出力は以下のとおりです。

<par def='1'>
   <run>This is start of line one para one </run>
   <run>text hotspot 1</run>
   <run> remainder of line one<break/></run>

   <run>This is line 2 </run>
   <run>another hotspot </run>
   <run>remainder of line 2 <break/></run>
 </par>

XSLT を使用して次の出力を生成することは可能ですか?

<document>
   <para>This is start of line one para one text hotspot 1 remainder of line one</para>

   <para>This is line 2 another hotspot remainder of line 2</para>
</document>

つまり、<break/>ノードは文の終わりを示しますが、文はいくつかの<run>ノードにまたがる場合があります。

ご参考までに、ソース データは Lotus Notes から DXL スキーマ形式で生成されます。

私はこれまでサードパーティのツールを使用して XSLT を生成してきました。喜んでコードを提供しますが、あまりクリーンではありません。

このフォーラムの大ファンになっていただき、ありがとうございます。

ドノ

4

3 に答える 3

1

これはどうですか?の最初の要素と、直前の要素にが含まれている場合にのみ、新しい<para>要素が作成されます。<run><par><par><break>

<xsl:template match="par">
  <xsl:for-each select="run[preceding-sibling::run[1]/break or not(preceding-sibling::run)]">
    <para>
      <xsl:apply-templates select="."/>
    </para>
  </xsl:for-each>
</xsl:template>

<xsl:template match="run">
  <xsl:value-of select="."/>
  <xsl:if test="not(break)">
    <xsl:apply-templates select="following-sibling::run[1]"/>
  </xsl:if>
</xsl:template>
于 2012-12-06T11:09:59.360 に答える
1

Though isn't a preferable method it works your way..

<?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" indent="yes"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

  <xsl:template match="par">
    <document>
      <para>
        <xsl:apply-templates select="node()"/>
      </para>
    </document>
  </xsl:template>

  <xsl:template match="run">
    <xsl:value-of select="."/>
    <xsl:apply-templates select="break"/>
  </xsl:template>

  <xsl:template match="break">
    <xsl:value-of select="'&#60;/para&#62;'" disable-output-escaping="yes"/>
    <xsl:value-of select="'&#60;para&#62;'" disable-output-escaping="yes"/>
  </xsl:template>
</xsl:stylesheet>
于 2012-12-06T11:47:46.863 に答える
0

この変換

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

 <xsl:key name="kPreceding" match="run"
      use="generate-id((following::break|descendant::break)[1])"/>

 <xsl:template match="par">
     <document>
      <xsl:apply-templates select="run/break"/>
     </document>
 </xsl:template>

 <xsl:template match="break">
  <para><xsl:apply-templates select="key('kPreceding', generate-id())/text()"/></para>
 </xsl:template>
</xsl:stylesheet>

提供されたXMLドキュメントに適用した場合:

<par def='1'>
    <run>This is start of line one para one </run>
    <run>text hotspot 1</run>
    <run> remainder of line one<break/></run>

    <run>This is line 2 </run>
    <run>another hotspot </run>
    <run>remainder of line 2<break/></run>
</par>

必要な正しい結果を生成します。

<document>
   <para>This is start of line one para one text hotspot 1 remainder of line one</para>
   <para>This is line 2 another hotspot remainder of line 2</para>
</document>

説明

これは、典型的なXSLT1.0位置グループ化ソリューションです。キーを使用して、要素と、それがグループとして識別するbreakすべての要素との関係を表します。run

于 2012-12-06T13:33:48.250 に答える