xml - 1 つの xslt 変換の結果が別の xslt 変換の入力になります

okwaves2024-01-25  10

私は xslt の初心者なので、基本的な質問をしても構いません。最初の変換レベルの結果を別の変換レベルの入力として使用したいと考えています。最初の変換の入力は、サブスクライバ (「aries」、「cas」...) を含む target.xml ファイルです。

<subscription-table>
        <subscriber name="aries" useZipXfer="yes" usePubMed="no">
            <ftp server="dummy.url1.com" port="21" remotedir="/XXXXXXX" userid="XXXXXXX" password="XXXXXXXXX" binary="yes" passive="no"/>
            <notify-success>
                <mail mailhost="smtprelayptc1.test.net" mailport="25" subject="MRCS-PS HEADER UPDATE - PAP">
                    <from address="[email protected]"/>
                    <to address="[email protected]"/>
                </mail>
            </notify-success>
            <notify-failure>
                <mail mailhost="smtprelayptc1.test.net" mailport="25" subject="MRCS-PS HEADER UPDATE - PAP">
                    <from address="[email protected]"/>
                    <to address="[email protected]"/>
                </mail>
            </notify-failure>
        </subscriber>
        <subscriber name="cas" useZipXfer="no" usePubMed="yes" sendPromote="no">
            <ftp server="dummy.url2.com" port="21" remotedir="/YYYYYYY" userid="YYYYYYY" password="YYYYYYYYY" binary="yes" passive="no"/>
            <notify-success>
                <mail mailhost="smtprelayptc1.test.net" mailport="25" subject="MRCS-PS HEADER UPDATE - PAP">
                    <from address="[email protected]"/>
                    <to address="[email protected]"/>
                </mail>
            </notify-success>
            <notify-failure>
                <mail mailhost="smtprelayptc1.test.net" mailport="25" subject="MRCS-PS HEADER UPDATE - PAP">
                    <from address="[email protected]"/>
                    <to address="[email protected]"/>
                </mail>
            </notify-failure>
        </subscriber>
</subscription-table>

各サブスクライバには独自のファイル (aries_subscriptions.xml、cas_subscriptions.xml など) があります。購読ファイルにはジャーナルが含まれます。たとえば、aries_subscriptions.xml は次のとおりです。

<subscription>
     <database status="update" name="ovftdb">        
         <journal status="*" name="00002030" >
             <issue status="new" pubdate="01/01/2004" name="*" an="99999999-999999999-99999" >
                 <header/>
             </issue>
         </journal>      
         <journal status="*" name="00000374" >
             <issue status="new" pubdate="01/01/2004" name="*" an="99999999-999999999-99999" >
                 <header/>
             </issue>
         </journal>      
         <journal status="*" name="00000372" >
             <issue status="new" pubdate="01/01/2004" name="*" an="99999999-999999999-99999" >
                 <header/>
             </issue>
         </journal>
...

3 番目のファイル lookup.xml を生成する必要があります。このファイルには、サブスクライバに応じて繰り返しジャーナルを含めることができます (サブスクライバは属性「identifier」内にあります)。

<?xml version="1.0" encoding="UTF-8"?>
<journals xmlns:xlink="http://www.w3.org/1999/xlink">
        <journal name="00002030">
            <target identifier="aries" pubdate="01/01/2004">
                <targetLink xmlns:xlink="http://www.w3.org/1999/xlink"
              xlink:href="targets.xml#xpointer(/descendant::subscriber[attribute::name='$subscriberNameFromDoc'])"/>
            </target>
        </journal>
        <journal name="00000372">
            <target identifier="aries" pubdate="01/01/2004">
                <targetLink xmlns:xlink="http://www.w3.org/1999/xlink"
              xlink:href="targets.xml#xpointer(/descendant::subscriber[attribute::name='$subscriberNameFromDoc'])"/>
            </target>
        </journal>
        <journal name="00002030">
            <target identifier="cas" pubdate="01/01/2004">
                <targetLink xmlns:xlink="http://www.w3.org/1999/xlink"
              xlink:href="targets.xml#xpointer(/descendant::subscriber[attribute::name='$subscriberNameFromDoc'])"/>
            </target>
        </journal>
        <journal name="00000372">
            <target identifier="cas" pubdate="01/01/2004">
                <targetLink xmlns:xlink="http://www.w3.org/1999/xlink"
              xlink:href="targets.xml#xpointer(/descendant::subscriber[attribute::name='$subscriberNameFromDoc'])"/>
            </target>
        </journal>
    </journals>

繰り返しジャーナルを作成する xslt コードを使用し、固有の情報を持つドキュメントを生成したいと考えています。入れ子になったすべてのサブスクライバを含むファイル。必要なドキュメントは次のとおりです。

<?xml version="1.0" encoding="UTF-8"?>
<journals xmlns:xlink="http://www.w3.org/1999/xlink"
    <journal name="00002030">
       <target identifier="aries" pubdate="01/01/2004">
          <targetLink xmlns:xlink="http://www.w3.org/1999/xlink"
              xlink:href="targets.xml#xpointer(/descendant::subscriber[attribute::name='$subscriberNameFromDoc'])"/>
       </target>
       <target identifier="cas" pubdate="01/01/2004">
          <targetLink xmlns:xlink="http://www.w3.org/1999/xlink"
              xlink:href="targets.xml#xpointer(/descendant::subscriber[attribute::name='$subscriberNameFromDoc'])"/>
       </target>
    </journal>
    <journal name="00000372">
       <target identifier="aries" pubdate="01/01/2004">
          <targetLink xmlns:xlink="http://www.w3.org/1999/xlink"
              xlink:href="targets.xml#xpointer(/descendant::subscriber[attribute::name='$subscriberNameFromDoc'])"/>
       </target>
       <target identifier="cas" pubdate="01/01/2004">
          <targetLink xmlns:xlink="http://www.w3.org/1999/xlink"
              xlink:href="targets.xml#xpointer(/descendant::subscriber[attribute::name='$subscriberNameFromDoc'])"/>
       </target>
    </journal>  
</journals>

重複したジャーナルを作成する xslt の最初の部分は次のとおりです。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:xlink="http://www.w3.org/1999/xlink"      
    xmlns:saxon="http://saxon.sf.net/"
    exclude-result-prefixes="xs xsl saxon xlink">


    <xsl:strip-space elements="*" />    

    <xsl:include href="utilities.xsl" />
    <xsl:include href="assetTargetToLookupRemoveDuplicates.xsl"/>

    <xsl:variable name="inputFolderAndFile">
        <xsl:call-template
            name="getValuesBeforeAndAfterLastOccurance">
            <xsl:with-param name="inputString"
                select="string(base-uri(.))" />
            <xsl:with-param name="char" select="'/'" />
        </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="inputFolderPath"
        select="substring-before($inputFolderAndFile, '*')" />
    <xsl:variable name="inputFile"
        select="substring-after($inputFolderAndFile, '*')" />
    <xsl:variable name="tokens"
        select="tokenize($inputFile,'\.')" />
    <xsl:variable name="baseFileName"
        select="string-join($tokens[not(.=$tokens[last()])], '.')" />

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

    <xsl:template match="text()" mode="package">
        <xsl:copy/> 
    </xsl:template>     

    <xsl:template match="/" mode="package">
        <xsl:result-document method="xml" omit-xml-declaration="no" indent="yes" encoding="UTF-8" href="lookup.xml">
            <xsl:variable name="initialOutput">             
                <xsl:apply-templates mode="package"/>               
            </xsl:variable>                                             
            <xsl:apply-templates select="$initialOutput" mode="remove-duplicates"/>                        
        </xsl:result-document>
    </xsl:template> 

    <xsl:template match="subscriber" mode="package">
        <xsl:variable name="subscriberName" select="document(concat($inputFolderPath, '/', @name,'_subscriptions.xml'))" />
        <xsl:element name="journals" namespace="http://www.w3.org/1999/xlink">      
            <xsl:apply-templates select="$subscriberName" mode="journal-processing"/>
        </xsl:element>      
    </xsl:template>

    <xsl:template match="journal" mode="journal-processing">
        <xsl:variable name="journalName" select="@name" />
        <xsl:variable name="journalPubDate" select="issue/@pubdate" />
        <xsl:variable name="subscriberNameFromDoc" select="substring-before(tokenize(base-uri(), '/')[last()], '_subscriptions')" />

        <xsl:element name="journal">
            <xsl:attribute name="name" select="$journalName"/>
            <xsl:element name="target">
                <xsl:attribute name="identifier" select="$subscriberNameFromDoc"/>
                <xsl:attribute name="pubdate" select="$journalPubDate"/>
                <xsl:element name="targetLink">                                                            
                    <xsl:attribute name="xlink:href">targets.xml#xpointer(/descendant::subscriber[attribute::name='$subscriberNameFromDoc'])</xsl:attribute>                    
                    <xsl:attribute name="xlink:href" select="targets.xml#//descendant::subscriber[attribute::name='$subscriberNameFromDoc']"/> -->
                </xsl:element>
            </xsl:element>
        </xsl:element>

    </xsl:template>
</xsl:stylesheet>

$initialOutput 変数を使用して、最初の変換の結果を次の変換に提供したいと考えています。 2 番目の変換、assetTargetToLookupRemoveDuplicates.xsl には、次のコードがあります。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:xlink="http://www.w3.org/1999/xlink"  
    xmlns:saxon="http://saxon.sf.net/"
    exclude-result-prefixes="xlink xs xsl saxon"
    version="2.0">


    <xsl:strip-space elements="*" />

    <xsl:template match="/" mode="remove-duplicates">       
        <xsl:apply-templates select="journals" mode="remove-duplicates"/>
    </xsl:template>

    <xsl:template match="journals" mode="remove-duplicates">
        <xsl:copy>
            <xsl:for-each-group select="journal" group-by="@name">
                <xsl:copy>
                    <xsl:copy-of select="@*"/>
                    <xsl:copy-of select="current-group()/target"/>
                </xsl:copy>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

2 番目の変換では、重複した仕訳帳を一意の仕訳帳に変換しません。プロセス全体の結果、重複した仕訳帳を含む文書が作成されます。結果は再び次のようになります。

<?xml version="1.0" encoding="UTF-8"?>
<journals xmlns:xlink="http://www.w3.org/1999/xlink">
        <journal name="00002030">
            <target identifier="aries" pubdate="01/01/2004">
                <targetLink xmlns:xlink="http://www.w3.org/1999/xlink"
              xlink:href="targets.xml#xpointer(/descendant::subscriber[attribute::name='$subscriberNameFromDoc'])"/>
            </target>
        </journal>
        <journal name="00000372">
            <target identifier="aries" pubdate="01/01/2004">
                <targetLink xmlns:xlink="http://www.w3.org/1999/xlink"
              xlink:href="targets.xml#xpointer(/descendant::subscriber[attribute::name='$subscriberNameFromDoc'])"/>
            </target>
        </journal>
        <journal name="00002030">
            <target identifier="cas" pubdate="01/01/2004">
                <targetLink xmlns:xlink="http://www.w3.org/1999/xlink"
              xlink:href="targets.xml#xpointer(/descendant::subscriber[attribute::name='$subscriberNameFromDoc'])"/>
            </target>
        </journal>
        <journal name="00000372">
            <target identifier="cas" pubdate="01/01/2004">
                <targetLink xmlns:xlink="http://www.w3.org/1999/xlink"
              xlink:href="targets.xml#xpointer(/descendant::subscriber[attribute::name='$subscriberNameFromDoc'])"/>
            </target>
        </journal>
    </journals>

2 番目の変換がなぜそのように機能するのかを理解するのを手伝っていただけますか?何かございましたら、よろしくお願いいたします。

1

Saxon のどのバージョンを使用していますか?モード パッケージを初期モードにすべきではないでしょうか?

– マーティン・ホネン

2020 年 9 月 5 日 6:14

サクソン語バージョンを正しい方法で入手できたかどうかわかりません。私のSaxonバージョンはSaxonEE9-6-0-10Jで、saxon9ee.jarを使用していると思います。モード「パッケージ」を変更しようとしました。初期モードに戻しましたが、役に立ちませんでした。改めてありがとうございます。

– nean0502

2020 年 9 月 5 日 22:02



------------------------

書き直す必要があると思います

<xsl:template match="/" mode="package">
    <xsl:result-document method="xml" omit-xml-declaration="no" indent="yes" encoding="UTF-8" href="lookup.xml">
        <xsl:variable name="initialOutput">             
            <xsl:apply-templates mode="package"/>               
        </xsl:variable>                                             
        <xsl:apply-templates select="$initialOutput" mode="remove-duplicates"/>                        
    </xsl:result-document>
</xsl:template> 

として

<xsl:variable name="initialOutput">             
     <xsl:apply-templates mode="package"/>               
</xsl:variable> 

<xsl:template match="/">
    <xsl:result-document method="xml" omit-xml-declaration="no" indent="yes" encoding="UTF-8" href="lookup.xml">                                   
        <xsl:apply-templates select="$initialOutput" mode="remove-duplicates"/>                        
    </xsl:result-document>
</xsl:template> 

4

マーティン、お時間を割いていただき、誠にありがとうございました。残念ながら、それは機能しません。最終処理の結果は空のドキュメントです。再度変更すると、<xsl:template match="/"<xsl:template match="/"> <xsl:template match="/" にモード=&quパッケージ」重複を削除せずに結果が得られました。また、モード「パッケージ」を変更しようとしました。初期モードに移行しましたが、役に立ちませんでした。

– nean0502

2020 年 9 月 5 日 21:59

@nean0502、ある時点でコードに <xsl:element name="journals" が含まれています。 namespace="http://www.w3.org/1999/xlink">そのため、XLink 名前空間にジャーナル要素が作成されます。すべての XSLT テンプレートは、名前空間のない要素を処理しているようです。したがって、名前空間違いは、出力が得られない主な理由の 1 つです。 2 番目のモードが期待する中間結果が実際に得られたかどうかを確認するために、デバッグ目的で中間結果を出力する必要がある場合があります。

– マーティン・ホネン

2020 年 9 月 6 日 10:58

マーティンさん、改めてありがとうございます。この問題を解決するために最善を尽くします。

– nean0502

2020 年 9 月 7 日 10:46

私のテンプレートが「重複を削除」することを理解しました。何らかの理由でまったく適用されません。

– nean0502

2020 年 9 月 7 日 11:08



------------------------

Martin のコメントと助けのおかげで、最終的に解決策を修正することができました。次のコードは、質問で説明したことを実行します。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:xlink="http://www.w3.org/1999/xlink"      
    xmlns:saxon="http://saxon.sf.net/"      
    exclude-result-prefixes="xs xsl saxon xlink">

    <xsl:strip-space elements="*" />

    <xsl:output method="xml" omit-xml-declaration="no"
        indent="yes" encoding="UTF-8" saxon:character-representation="entity" />

    <xsl:include href="utilities.xsl" />    

    <xsl:variable name="inputFolderAndFile">
        <xsl:call-template
            name="getValuesBeforeAndAfterLastOccurance">
            <xsl:with-param name="inputString"
                select="string(base-uri(.))" />
            <xsl:with-param name="char" select="'/'" />
        </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="inputFolderPath"
        select="substring-before($inputFolderAndFile, '*')" />
    <xsl:variable name="inputFile"
        select="substring-after($inputFolderAndFile, '*')" />
    <xsl:variable name="tokens"
        select="tokenize($inputFile,'\.')" />
    <xsl:variable name="baseFileName"
        select="string-join($tokens[not(.=$tokens[last()])], '.')" />

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

    <xsl:template match="text()" >
        <xsl:copy/> 
    </xsl:template>     


    <xsl:template match="/">        
        <xsl:variable name="initialOutput">
            <xsl:element name="journals">
                <xsl:namespace name="xlink" select="'http://www.w3.org/1999/xlink'"/>       
                <xsl:apply-templates/>
            </xsl:element>          
        </xsl:variable>                                                                     
        <xsl:apply-templates select="$initialOutput" mode="remove-duplicates"/>  
    </xsl:template> 

    <xsl:template match="subscriber">       
        <xsl:variable name="subscriberName" select="document(concat($inputFolderPath, '/', @name,'_subscriptions.xml'))" />     
        <xsl:apply-templates select="$subscriberName" mode="journal-processing"/>       
    </xsl:template>

    <xsl:template match="journal" mode="journal-processing">
        <xsl:variable name="journalName" select="@name" />
        <xsl:variable name="journalPubDate" select="issue/@pubdate" />
        <xsl:variable name="subscriberNameFromDoc" select="substring-before(tokenize(base-uri(), '/')[last()], '_subscriptions')" />

        <xsl:element name="journal">
            <xsl:attribute name="name" select="$journalName"/>
            <xsl:element name="target">
                <xsl:attribute name="identifier" select="$subscriberNameFromDoc"/>
                <xsl:attribute name="pubdate" select="$journalPubDate"/>
                <xsl:element name="targetLink" namespace="http://www.w3.org/1999/xlink">
                                        <xsl:variable name="apostrophe">'</xsl:variable>                
                    <xsl:attribute name="xlink:href" select="concat('targets.xml','#','xpointer(/descendant::subscriber[attribute::name=',$apostrophe,$subscriberNameFromDoc,$apostrophe,'])')"/>
                </xsl:element>
            </xsl:element>
        </xsl:element>

    </xsl:template> 

    <xsl:template match="/" mode="remove-duplicates">           
        <xsl:apply-templates select="journals" mode="remove-duplicates"/>
    </xsl:template>

    <xsl:template match="journals" mode="remove-duplicates">                    
        <xsl:copy>
            <xsl:for-each-group select="journal" group-by="@name">
                <xsl:copy>
                    <xsl:copy-of select="@*"/>
                    <xsl:copy-of select="current-group()/target"/>
                </xsl:copy>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

総合生活情報サイト - OKWAVES
総合生活情報サイト - OKWAVES
生活総合情報サイトokwaves(オールアバウト)。その道のプロ(専門家)が、日常生活をより豊かに快適にするノウハウから業界の最新動向、読み物コラムまで、多彩なコンテンツを発信。