0

私の入力は次のとおりです。

<w:body>
 <w:p>
 <w:pPr>
    <w:pStyle w:val="paragraph"/>
  </w:pPr>
 <w:r><w:t>1274394 The milk costs , $1.99 [12] test Figure 1, Table 1</w:t></w:r>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="paragraph"/>
</w:pPr>
<w:r><w:t>sample text Figure 1 and [1]</w:t></w:r>
</w:p>
</w:body>

XSLT「analyze-string」を使用して以下のような出力を取得したい

<w:body>
<w:p>
<w:pPr>
   <w:pStyle w:val="paragraph"/>
 </w:pPr>
<w:r><w:t>1274394 The milk costs , $1.99 <ref>[12]</ref> test <fig>Figure 1</fig>, <tab>Table 1</tab></w:t></w:r>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="paragraph"/>
</w:pPr>
<w:r><w:t>sample text Figure 1 and [1]</w:t></w:r>
</w:p>
</w:body>

XSLTは以下のとおりです。

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

<xsl:template match="w:t/text()">
 <xsl:variable name="phase1">
  <xsl:apply-templates select="." mode="fig" />
 </xsl:variable>
 <xsl:variable name="phase2">
  <xsl:apply-templates select="." mode="tab" />
 </xsl:variable>
  <xsl:apply-templates select="$phase1" mode="ref" />
</xsl:template>


<xsl:template match="text()" mode="fig">
 <xsl:analyze-string select="." regex="Figure (\d{{1,2}})">
 <xsl:matching-substring>
  <fig>
   <xsl:value-of select="." />
  </fig>
 </xsl:matching-substring>
 <xsl:non-matching-substring>
  <xsl:value-of select="." />
 </xsl:non-matching-substring>
 </xsl:analyze-string>
</xsl:template>

<xsl:template match="text()" mode="ref">
 <xsl:analyze-string select="." regex="\[(\d{{1,2}})\]">
 <xsl:matching-substring>
  <ref>
   <xsl:value-of select="." />
  </ref>
 </xsl:matching-substring>
 <xsl:non-matching-substring>
  <xsl:value-of select="." />
 </xsl:non-matching-substring>
 </xsl:analyze-string>
</xsl:template>
    <xsl:template match="text()" mode="tab">
 <xsl:analyze-string select="." regex="Table (\d{{1,2}})">
 <xsl:matching-substring>
  <tab>
   <xsl:value-of select="." />
  </tab>
 </xsl:matching-substring>
 <xsl:non-matching-substring>
  <xsl:value-of select="." />
 </xsl:non-matching-substring>
 </xsl:analyze-string>
</xsl:template>
<xsl:template match="@*|*|comment()|processing-instruction()" mode="ref">
 <xsl:copy>
  <xsl:apply-templates select="@*|node()" mode="ref"/>
 </xsl:copy>
</xsl:template>
</xsl:stylesheet>      

上記のXSLTを使用して、Figとrefを置き換えることができます。テーブルを置き換えるために、Phase2変数を使用していますが、出力が得られません。これを行う別の方法はありますか?

4

1 に答える 1

0

パイプライン設計のポイントを逃したようです。パイプラインでは、あるステージ/フェーズの出力を次のステージの入力にフィードします。

同じパイプライン設計を維持したい場合は、このXSLT2.0スタイルシートを使用してください...

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fn="http://www.w3.org/2005/xpath-functions"
  xmlns:w="www"
  exclude-result-prefixes='xsl fn'>
<xsl:output method="xml" indent="yes"/>

<xsl:template match="@*|node()">
 <xsl:copy>
  <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
</xsl:template>
      
<xsl:template match="w:t/text()">
 <xsl:variable name="phase1">
  <xsl:apply-templates select="." mode="fig" />
 </xsl:variable>
 <xsl:variable name="phase2">
  <xsl:apply-templates select="$phase1" mode="tab" />
 </xsl:variable>
  <xsl:apply-templates select="$phase2" mode="ref" />
</xsl:template>

<xsl:template match="text()" mode="fig">
 <xsl:analyze-string select="." regex="Figure (\d{{1,2}})">
 <xsl:matching-substring>
  <fig>
   <xsl:value-of select="." />
  </fig>
 </xsl:matching-substring>
 <xsl:non-matching-substring>
  <xsl:value-of select="." />
 </xsl:non-matching-substring>
 </xsl:analyze-string>
</xsl:template>

<xsl:template match="text()" mode="tab">
 <xsl:analyze-string select="." regex="Table (\d{{1,2}})">
 <xsl:matching-substring>
  <tab>
   <xsl:value-of select="." />
  </tab>
 </xsl:matching-substring>
 <xsl:non-matching-substring>
  <xsl:value-of select="." />
 </xsl:non-matching-substring>
 </xsl:analyze-string>
</xsl:template>

<xsl:template match="text()" mode="ref">
 <xsl:analyze-string select="." regex="\[(\d{{1,2}})\]">
 <xsl:matching-substring>
  <ref>
   <xsl:value-of select="." />
  </ref>
 </xsl:matching-substring>
 <xsl:non-matching-substring>
  <xsl:value-of select="." />
 </xsl:non-matching-substring>
 </xsl:analyze-string>
</xsl:template>

<xsl:template match="@*|*|comment()|processing-instruction()" mode="tab">
 <xsl:copy>
  <xsl:apply-templates select="@*|node()" mode="tab"/>
 </xsl:copy>
</xsl:template>
      
<xsl:template match="@*|*|comment()|processing-instruction()" mode="ref">
 <xsl:copy>
  <xsl:apply-templates select="@*|node()" mode="ref"/>
 </xsl:copy>
</xsl:template>
      
</xsl:stylesheet>  

ただし、テキストノードで3つの同様の変換を行う場合は、一般的なパラメータ化されたテンプレートデザインに移動し、fig / tab/refタイプの要素とそのテキストパターンのルックアップテーブルにアクセスすることをお勧めします。時間によっては、この質問をそのようなデザインのスタイルシートで更新する場合としない場合があります。

アップデート

これがcommponテンプレートで設計されたスタイルシートです...

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fn="http://www.w3.org/2005/xpath-functions"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:so="http://stackoverflow.com/questions/11734596"
  xmlns:w="www"
  exclude-result-prefixes='xsl fn xs so'>
<xsl:output method="xml" indent="yes"/>

<xsl:template match="@*|node()">
 <xsl:copy>
  <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
</xsl:template>
      
<xsl:template match="w:t/text()">
  <xsl:call-template name="parse-text">
   <xsl:with-param name="raw-text" select="." />
  </xsl:call-template>  
</xsl:template>

<so:markup-lexicon>
 <so:map pattern="Figure (\d{1,2})" markup="fig" />
 <so:map pattern="Table (\d{1,2})"  markup="tab" />
 <so:map pattern="\[(\d{1,2})\]"    markup="ref" />
</so:markup-lexicon>

<xsl:template name="parse-text">
 <xsl:param name="raw-text" as="xs:string" />
 <xsl:variable name="m" select="
   document('')/*/so:markup-lexicon/so:map
   [fn:matches(current(),@pattern)]" as="element()*"/>
 <xsl:choose> 
  <xsl:when test="$m">
   <xsl:call-template name="analyze-wp-text">
    <xsl:with-param name="raw-text" select="." />
    <xsl:with-param name="markup"   select="$m[1]/@markup" />
    <xsl:with-param name="pattern"  select="$m[1]/@pattern" />
   </xsl:call-template>
  </xsl:when> 
  <xsl:otherwise> 
   <xsl:value-of select="$raw-text" />
  </xsl:otherwise> 
 </xsl:choose> 
</xsl:template>

<xsl:template name="analyze-wp-text">
 <xsl:param name="raw-text" as="xs:string" />
 <xsl:param name="markup"   as="xs:string" />
 <xsl:param name="pattern"  as="xs:string" />
 <xsl:analyze-string select="$raw-text" regex="{$pattern}">
 <xsl:matching-substring>
  <xsl:element name="{$markup}">
   <xsl:value-of select="." />
  </xsl:element>
 </xsl:matching-substring>
 <xsl:non-matching-substring>
  <xsl:call-template name="parse-text">
   <xsl:with-param name="raw-text" select="." />
  </xsl:call-template>  
 </xsl:non-matching-substring>
 </xsl:analyze-string>
</xsl:template>
      
</xsl:stylesheet> 

両方のスタイルシートはSaxonでテストされており、これと同じ同じ出力を生成します...

<w:body xmlns:w="www">
   <w:p>
      <w:pPr>
         <w:pStyle w:val="paragraph"/>
      </w:pPr>
      <w:r>
         <w:t>1274394 The milk costs , $1.99 <ref>[12]</ref> test <fig>Figure 1</fig>, <tab>Table 1</tab>
         </w:t>
      </w:r>
   </w:p>
   <w:p>
      <w:pPr>
         <w:pStyle w:val="paragraph"/>
      </w:pPr>
      <w:r>
         <w:t>sample text <fig>Figure 1</fig> and <ref>[1]</ref>
         </w:t>
      </w:r>
   </w:p>
</w:body>  
于 2012-07-31T07:24:17.873 に答える