2016-07-01 3 views
0

XMLファイルから '(シングルクォート)と#を削除します。私はすべてのXMLノードをチェックする必要があります。私はすでに他のデータ抽出を行っているXSLTテンプレートを持っています。だから私は同じXSLTで別のテンプレートを作成することはできません。また、私はすべてのノードでtranslate関数を呼び出す必要はありません。すべてのノードで翻訳機能を適用する簡単な方法はありますか?最初のテンプレートが抽出され、2番目のテンプレートがデータを変換する同じXSLTで2つのテンプレートを適用できますか?新しいTempleteを作成せずにXSLTを使用して、すべてのXMLノードから#文字を削除します

誰かが私を助けることができますか? XMLサンプル:

<Report> 
    <ResCountryCode>US</ResCountryCode> 
    <Reporting> 
     <TIN> 
      <TaxIDNo>12456-451</TaxIDNo> 
      <TINCountryCode>US</TINCountryCode> 
     </TIN> 
     <TIN> 
      <TaxIDNo>4454-8754-4</TaxIDNo> 
      <TINCountryCode>US</TINCountryCode> 
     </TIN> 
     <OrganizationName>AB'C Ca'r Limi#ted</OrganizationName> 
     .... 
     ....(other nodes with same ' and # character) 
    </Reporting> 

</Report> 

XSLTサンプル:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" /> 
    <xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()" /> 
    </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

<xsl:template match="Report"> 
    <Finance> 
     <Reporting> 
      <xsl:element name="ResCode"> 
       <xsl:value-of select="ResCountryCode" /> 
       </xsl:element> 
       <xsl:for-each select="Reporting/TIN"> 
       <xsl:element name="TIN"> 
        <xsl:attribute name="issuedBy"> 
        <xsl:value-of select="TINCountryCode" /> 
        </xsl:attribute> 
        <xsl:value-of select="TaxIDNo" /> 
       </xsl:element> 
       </xsl:for-each> 
       <xsl:element name="Name"> 
       <xsl:value-of select="Reporting/OrganizationName" /> 
       </xsl:element> 
     </Reporting> 
    </Finance> 
</xsl:template> 

出力:

<Report> 
    <Finance> 
     <ResCountryCode>US</ResCountryCode> 
     <Reporting> 
      <sfa:TIN issuedBy="US">12456-451</sfa:TIN> 
      <sfa:TIN issuedBy="US">4454-8754-4</sfa:TIN> 
      <Name>AB'C Ca'r Limi#ted</Name> 
      .... 
      .... 
     </Reporting> 
    </Finance> 
</Report> 

予想される出力:

<Report> 
    <Finance> 
     <ResCountryCode>US</ResCountryCode> 
     <Reporting> 
      <sfa:TIN issuedBy="US">12456-451</sfa:TIN> 
      <sfa:TIN issuedBy="US">4454-8754-4</sfa:TIN> 
      <Name>ABC Car Limited</Name> 
      .... 
      ....(with out ' and #) 
     </Reporting> 
    </Finance> 
</Report> 

答えて

1

の代わりにやってxsl:value-ofトンを使用してOノードの値を取得し、そのようxsl:apply-templatesを使用します。

<xsl:apply-templates select="Reporting/OrganizationName/text()" /> 

を次に、行うにtext()テンプレートマッチングを持っている一つの場所に翻訳し

優先順位がされ、それを保証するために、 IDテンプレートの前に一致します。

はXSLTでこの種のものを行うための最善の方法は、変換のパイプラインである

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

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

    <xsl:template match="text()" priority="2"> 
    <xsl:value-of select='translate(., "#&apos;", "")' /> 
    </xsl:template> 

    <xsl:template match="Report"> 
    <Finance> 
     <Reporting> 
      <xsl:element name="ResCode"> 
       <xsl:apply-templates select="ResCountryCode/text()" /> 
       </xsl:element> 
       <xsl:for-each select="Reporting/TIN"> 
       <xsl:element name="TIN"> 
        <xsl:attribute name="issuedBy"> 
         <xsl:apply-templates select="TINCountryCode/text()" /> 
        </xsl:attribute> 
        <xsl:value-of select="TaxIDNo" /> 
       </xsl:element> 
       </xsl:for-each> 
       <xsl:element name="Name"> 
        <xsl:apply-templates select="Reporting/OrganizationName/text()" /> 
       </xsl:element> 
     </Reporting> 
    </Finance> 
</xsl:template> 
</xsl:stylesheet> 
+0

こんにちはTim、ありがとうございました。それは完璧に機能します。 – Shuvra

2

このXSLTを試してみてください。入力に対して1回のパスで複数の変換を実行しないでください。 1つの単純な作業を行う変換のパイプラインを作成します。パイプラインの各ステップを他のパイプラインで再利用できるため、コードはずっと簡単でデバッグが容易で、再利用可能になります。 (これはよく知られていると思えば、UNIXシェルプログラミングの基本概念です)。

関連する問題