-2

xsltを使用して次のデータから不要なテキストを削除したい

Address>
<Rowinfo>
<LocatorDesignator>Dwelling  (Part Of) Null</LocatorDesignator>
<LocatorName>Flat  - Buena Villa House</LocatorName>
</Rowinfo>
<Rowinfo>
<LocatorDesignator>Flat  - Buena Villa House 1</LocatorDesignator>
<LocatorName>Flat  3a  Anderson's House</LocatorName>
</Rowinfo>
<Rowinfo>
<LocatorDesignator>Offices Unit 2a Funlife Building 02a</LocatorDesignator>
<LocatorName>office Unit 2a   Funlife Building  <LocatorName>
 </Rowinfo>
 </Address>

これを生成するには

<LocatorDesignator>Dwelling(Part Of)</LocatorDesignator>
<LocatorName>Buena Villa House</LocatorName>

<LocatorDesignator>Flat 1</LocatorDesignator>
<LocatorName> Anderson's House</LocatorName>

<LocatorDesignator>office Unit 2a</LocatorDesignator>
<LocatorName> Funlife Building  <LocatorName>

ロケーター名:抽出

  • ブエナビラハウス

  • アンダーソンの家

  • ファンライフビルディング

つまり、二重引用符で囲まれた部分は削除されます。

(row1) "Flat  -" Buena Villa House.   
(row2) "Flat  3a"  Anderson's House .
(row3) "office Unit 2a"   Funlife Building.

Locatordesignatorの場合:削除

  • ヌル

  • ブエナビラハウス

  • ファンライフビルディング

言い換えると、二重引用符で囲まれた部分は削除されます。

(row1) Dwelling  (Part Of) "Null"
(row2) Flat  " Buena Villa House" 1.   
(row3) office Unit 2a   "Funlife Building 02a". 
4

2 に答える 2

1

この完全な変換

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

 <my:LocatornameDeletes>
  <del>Flat  -</del>
  <del>Flat  3a</del>
  <del>office Unit 2a</del>
 </my:LocatornameDeletes>

 <my:LocatordesignatorDeletes>
  <del>Null</del>
  <del>Buena Villa House</del>
  <del>Funlife Building 02a</del>
 </my:LocatordesignatorDeletes>

 <xsl:variable name="vLocNameDels" select="document('')/*/my:LocatornameDeletes/*"/>    
 <xsl:variable name="vLocDesDels" select="document('')/*/my:LocatordesignatorDeletes/*"/>   
 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="LocatorName/text()">
  <xsl:call-template name="makeDeletes">
   <xsl:with-param name="pDeletes" select="$vLocNameDels"/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template match="LocatorDesignator/text()">
  <xsl:call-template name="makeDeletes">
   <xsl:with-param name="pDeletes" select="$vLocDesDels"/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template name="makeDeletes">
  <xsl:param name="pText" select="."/>
  <xsl:param name="pDeletes"/>

  <xsl:variable name="vDelText" select="$pDeletes[contains($pText, .)][1]"/>
  <xsl:if test="$vDelText">
   <xsl:variable name="vRough">
     <xsl:value-of select="substring-before($pText, $vDelText)"/>
     <xsl:value-of select="substring-after($pText, $vDelText)"/>
   </xsl:variable>

   <xsl:value-of select="normalize-space($vRough)"/>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

(不正を修正して)提供されたXMLドキュメントに適用した場合

<Address>
    <Rowinfo>
        <LocatorDesignator>Dwelling  (Part Of) Null</LocatorDesignator>
        <LocatorName>Flat  - Buena Villa House</LocatorName>
    </Rowinfo>
    <Rowinfo>
        <LocatorDesignator>Flat  - Buena Villa House 1</LocatorDesignator>
        <LocatorName>Flat  3a  Anderson's House</LocatorName>
    </Rowinfo>
    <Rowinfo>
        <LocatorDesignator>Offices Unit 2a Funlife Building 02a</LocatorDesignator>
        <LocatorName>office Unit 2a   Funlife Building  </LocatorName>
    </Rowinfo>
</Address>

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

<Address>
   <Rowinfo>
      <LocatorDesignator>Dwelling (Part Of)</LocatorDesignator>
      <LocatorName>Buena Villa House</LocatorName>
   </Rowinfo>
   <Rowinfo>
      <LocatorDesignator>Flat - 1</LocatorDesignator>
      <LocatorName>Anderson's House</LocatorName>
   </Rowinfo>
   <Rowinfo>
      <LocatorDesignator>Offices Unit 2a</LocatorDesignator>
      <LocatorName>Funlife Building</LocatorName>
   </Rowinfo>
</Address>

説明

  1. のテキストノードの子で削除される文字列LocatorNameは、グローバルレベルの要素で提供されますmy:LocatornameDeletes

  2. のテキストノードの子で削除される文字列LocatorDesignatorは、グローバルレベルの要素で提供されますmy:LocatordesignatorDeletes

  3. IDルールは、実行対象として選択されたすべてのノードを「現状のまま」コピーします。

  4. IDテンプレートを上書きする2つのテンプレートがあります。1つは一致LocatorName/text()し、もう1つは一致しLocatorDesignator/text()ます。これらの各テンプレートは、単にテンプレートを呼び出し、makeDeletes削除するサブストリングを含む正しいノードセットをパラメーターとして渡します。

  5. テンプレートは、パラメーターで渡されたサブストリングのmakeDeletes実際の「削除」を実行し$pDeletesます。これは、標準のXPath関数substring-before()との組み合わせを使用して実行されますsubstring-after()

于 2012-08-26T14:36:46.203 に答える
0

XSLT 1.0でこれらの文字列関数を使用して、目標を達成できます。

  1. contains()
  2. substring()
  3. substring-before()
  4. substring-after()

関数名をXSLTと一緒にグーグルで検索して、各関数の仕様を取得します。

XSLT 2.0にアップグレードできるのであれば、絶対にお勧めします。XSLT 2.0では、正規表現を使用して目標をはるかに簡単に達成できるようになります。

この入力ドキュメントで...

<t>
 <row>"Flat  -" Buena Villa House.</row>  
 <row>"Flat  3a"  Anderson's House .</row>
 <row>"office Unit 2a"   Funlife Building.</row>
</t>

...このXSLT1.0スタイルシートが上記のドキュメントに適用されると、二重引用符で囲まれたテキストフラグメントが削除されます...

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

<xsl:template match="/">
 <t>
   <xsl:apply-templates select="*/row"/>
 </t>
</xsl:template>

<xsl:template match="row">
 <row>
  <xsl:call-template name="remove-quoted-bit">
    <xsl:with-param name="raw-text" select="." />
  </xsl:call-template>  
 </row>
</xsl:template>
<xsl:template name="remove-quoted-bit">
  <xsl:param name="raw-text" />
  <xsl:choose>
    <xsl:when test="contains($raw-text,'&quot;')">
      <xsl:value-of select="concat(
        substring-before($raw-text,'&quot;'),
        substring-after( substring-after($raw-text,'&quot;'),'&quot;')
        )" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$raw-text" />
    </xsl:otherwise>  
  </xsl:choose>  
</xsl:template>  

</xsl:stylesheet>

...そしてこの出力を生成します...

<t>
  <row> Buena Villa House.</row>
  <row>  Anderson's House .</row>
  <row>   Funlife Building.</row>
</t>

アップデート

文法を編集しました。

于 2012-08-26T13:39:14.633 に答える