0

いくつかの XML ドキュメントをクリーンアップし、対応する ID のないすべての IDREF を削除しようとしています。なんらかの理由で、このばかげた問題を解決するための構文がわかりません。こんな事になると思っていたのに…

<xsl:template match="*">
 <xsl:variable name="id_list" select="@id"/>
 <xsl:if test="ref[not(contains($id_list, ./@rid))]">
   <!-- do nothing -->
 </xsl:if>
 <xsl:copy>
  <xsl:apply-templates select="node()|@*"/>
 </xsl:copy>
</xsl:template>
  • ref は要素名で、@rid は refid です。

サンプル入力は次のようなものになります...

<?xml version="1.0" encoding="iso-8859-1"?>
<article>
 <bdy>
  <p>In the second category [<ref rid="bibtts2009060795101" type="bib">2</ref>] and third category [<ref rid="bibtts2009060795102" type="bib">3</ref>]</p>
 </bdy>
 <bib>
  <bb pubtype="article" reftype="nonieee" id="bibtts2009060795101"><au sequence="first"><fnm>T.</fnm><snm>Smith</snm></au></bb>
 </bib>
</article>

2 番目の参照<ref rid="bibtts2009060795102" type="bib">3</ref>は削除されます

4

1 に答える 1

0
<xsl:variable name="id_list" select="@id"/>

テンプレート内では、最大で 1 つのノード(テンプレート内のコンテキスト ノードである (単一の) 要素$id_listの属性) を含むノード セットに変数が設定されます。idより良いアプローチは、スタイルシートの最上位でキーidを定義して、各値を対応する要素にマップすることです。

<xsl:key name="elementById" match="*[@id]" use="@id" />

次に、特定の refid 値を指定すると、idを使用して一致するものがあるかどうかをすばやく判断できますkey('elementById', 'theRefValue')。完全な例を次に示します。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:key name="elementById" match="*[@id]" use="@id" />

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

  <!-- but ignore all ref elements whose @rid does not
       correspond to any id -->
  <xsl:template match="ref[not(key('elementById', @rid))]" />
</xsl:stylesheet>

サンプル文書 (のわずかにワードラップされたバージョン) に適用した場合

<?xml version="1.0" encoding="iso-8859-1"?>
<article>
 <bdy>
  <p>In the second category [<ref rid="bibtts2009060795101" type="bib">2</ref>]
     and third category [<ref rid="bibtts2009060795102" type="bib">3</ref>]</p>
 </bdy>
 <bib>
  <bb pubtype="article" reftype="nonieee" id="bibtts2009060795101"><au sequence="first"><fnm>T.</fnm><snm>Smith</snm></au></bb>
 </bib>
</article>

生み出すだろう

<?xml version="1.0" encoding="iso-8859-1"?>
<article>
 <bdy>
  <p>In the second category [<ref rid="bibtts2009060795101" type="bib">2</ref>]
     and third category []</p>
 </bdy>
 <bib>
  <bb pubtype="article" reftype="nonieee" id="bibtts2009060795101"><au sequence="first"><fnm>T.</fnm><snm>Smith</snm></au></bb>
 </bib>
</article>
于 2012-12-14T16:23:46.807 に答える