3

いくつかの特定のタグを取り除くための最良の解決策は何かと格闘してきました。現在、私はいくつかの正規表現で繰り返し検索/置換を使用していますが、確かにもっと良い方法が必要です。xsltで直接行う方法が明確ではありません。

次の例を見てください:

<local xml:lang="en">[Some Indicator]<div class="tab"/>some more content here</local>

私はこれらのかなりの数を持っており、すべて同じ構造に従います。 [Some Indicator] は一種のリスト識別子であり、次のいずれかになります。

  • 1 つ以上の数字、場合によってはドットが続く
  • 1 文字、場合によってはその後にハイフンと別の文字が続く
  • 特定のコードポイント範囲 (この場合は 57600 から 58607) の 1 文字
  • 上記のバリエーションである他のいくつか

手動で数百回検索/置換することなく、これらすべてを取り除きたいです。xsl:analyze-string を試してみましたが、位置を気にせずにすべてを置き換えます。

いくつかの例 :

<some_nodes_above>
<local xml:lang="en">1<div class="tab"/>some more content here</local>
<local xml:lang="en">2.<div class="tab"/>some more content here</local>
<local xml:lang="fr">2-A<div class="tab"/>some more content here</local>
<local xml:lang="de">&#57600;<div class="tab"/>some more content here</local>
</some_nodes_above>

になる必要があります:

<some_nodes_above>
<local xml:lang="en">some more content here</local>
<local xml:lang="en">some more content here</local>
<local xml:lang="fr">some more content here</local>
<local xml:lang="de">some more content here</local>
</some_nodes_above>

だから私は、「ローカルノードの後に​​特定のインジケーターとタブ div が続くのを見るたびに、インジケーターとタブ div を取り除く」のような xslt(2) スクリプトを探しています。例の完全な解決策を探しているのではなく、私を正しい方向に導くためのものです。1 つのパターンでどのように機能するかを知っていれば、残りはおそらく自分で把握できます。

前もって感謝します。

4

2 に答える 2

2

この変換

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

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

 <xsl:template match=
  "local/node()[1]
               [self::text()
          and
            following-sibling::node()[1]
               [self::div and @class eq 'tab']
              and
               (
                matches(., '^(\d\.?)|(.\-.)$')
               or
                 string-length(.) eq 1
                and
                 string-to-codepoints(.) ge 57600
                and
                 string-to-codepoints(.) le 58607
                )
               ]"/>

 <xsl:template match=
  "div[@class eq 'tab'
     and
       preceding-sibling::node()[1]
               [self::text()
              and
               (
                matches(., '^(\d\.?)|(.\-.)$')
               or
                 string-length(.) eq 1
                and
                 string-to-codepoints(.) ge 57600
                and
                 string-to-codepoints(.) le 58607
                )
               ]
      ]"/>
</xsl:stylesheet>

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

<some_nodes_above>
    <local xml:lang="en"
     >1<div class="tab"/>some more content here</local>
    <local xml:lang="en"
     >2.<div class="tab"/>some more content here</local>
    <local xml:lang="fr"
     >2-A<div class="tab"/>some more content here</local>
    <local xml:lang="de"
     >&#57600;<div class="tab"/>some more content here</local>
</some_nodes_above>

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

<some_nodes_above>
   <local xml:lang="en">some more content here</local>
   <local xml:lang="en">some more content here</local>
   <local xml:lang="fr">some more content here</local>
   <local xml:lang="de">some more content here</local>
</some_nodes_above>
于 2012-07-01T20:47:26.500 に答える
2

空の文字列に置き換え(?<=<local xml:lang="\w+">).+<div class="tab"/>ます正規表現オプションの複数行を含めます

于 2012-07-01T19:46:27.213 に答える