2017-06-14 5 views
0

私は1つの非常に複雑なXMLファイルを持っていて、複雑なXMLファイルをいくつか修正しています。複雑なものから XSLT - 2つのXMLファイルを比較して組み合わせる

一つの要素:単純なものから

<h1:Document DocKey="obj  40020528"> 
     <h1:Block Type="obj" 
       CreationDate="20.03.2014 09:39:50" 
       CreatorID="Admin" 
       ChangeDate="21.01.2015 14:40:51" 
       ChangerID="Admin" 
       OwnerID="Admin" 
       FieldsCount="17"> 
     <h1:Field Type="5000" Value="40020528"/> 
     <h1:Field Type="5060" Value="Aufnahme"> 
      <h1:Field Type="5064" Value="unbekannt"/> 
     </h1:Field> 
     <h1:Field Type="ob28" Value="Verwalter"> 
      <h1:Field Type="2864" Value="Köln"/> 
      <h1:Field Type="2900" 
         Value="Theaterwissenschaftliche Sammlung, Universität zu Köln"/> 
      <h1:Field Type="2930" Value="Fotoabteilung"/> 
      <h1:Field Type="2950" Value="TWS_FGL00541"/> 
      <h1:Field Type="907d" Value="No: 4260"/> 
     </h1:Field> 
     <h1:Field Type="ob26" Value="Aufnahmeort"> 
      <h1:Field Type="2664" Value="unbekannt"/> 
      <h1:Field Type="2690" Value="Theater"/> 
      <h1:Field Type="2700" Value="Theater unbekannt"/> 
     </h1:Field> 
     <h1:Field Type="ob30" Value="Herstellung"> 
      <h1:Field Type="3100" Value="unbekannt"/> 
      <h1:Field Type="3475" Value="Fotograf/in, Atelier"/> 
     </h1:Field> 
     <h1:Field Type="5007" Value="Darstellung"> 
      <h1:Field Type="5009" Value="unbekannt"/> 
      <h1:Field Type="5010" Value="Autor/in"/> 
      <h1:Field Type="5013" Value="xTITELx"/> 
     </h1:Field> 
     <h1:Field Type="ob40" Value="Inszenierung"> 
      <h1:Field Type="4100" Value="unbekannt"/> 
      <h1:Field Type="4475" Value="Regie"/> 
     </h1:Field> 
     <h1:Field Type="ob40" Value="Inszenierung"> 
      <h1:Field Type="4100" Value="unbekannt"/> 
      <h1:Field Type="4475" Value="Bühnenbild"/> 
     </h1:Field> 
     <h1:Field Type="5200" Value="Fritz Feinhals"/> 
     <h1:Field Type="5220" Value="Fotografie"/> 
     <h1:Field Type="5230" Value="Negativ"/> 
     <h1:Field Type="5240" Value="Glasplattennegativ"/> 
     <h1:Field Type="5360" Value="18x13"/> 
     <h1:Field Type="55th" Value="Feinhals, Fritz"/> 
     </h1:Block> 
    </h1:Document> 

一つの要素:

<signatur id="TWS_FGL00541"> 
     <datum/> 
     <ort/> 
     <ortsteil/> 
     <titel>Fritz Feinhals als Wotan</titel> 
     <fotograf/> 
     <komponist>Wagner, Richard</komponist> 
     <author/> 
     <regie/> 
     <buehnenbild/> 
     <darPerson/> 
     <freiText>Walküre</freiText> 
    </signatur> 

私は「Signatur」ごとに2つの文書の要素を比較しようとしています。署名が同じであれば、修正があります。修正がある場合、単純なXMLのどの子要素を複合要素の子要素に入れなければならないかをチェックする必要があるため、単純な要素の子要素に値があるかどうかをチェックする必要があります。そのように、値は複雑な要素の特定の要素に置かれなければなりません。

私はそれを行うための最良の方法は何も知らない。次のように私のような何かをしようとしていた。

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

<xsl:variable name="Halft" select="document('simpleOne.xml')" /> 

<xsl:variable name="Sig_Halft" select="preceding-sibling::$Halft/document/signatur" /> 
<xsl:variable name="Sig_HiDA" select="preceding-sibling::DocumentSet/Document/Block/Field[@Type='ob28']/Field[@Type='2950']/@Value" /> 


<!-- <xsl:variable name="" select="" /> --> 

<xsl:template match="DocumentSet/Document"> 
    <xsl:if test="$Sig_HiDA = $Sig_Halft"> <!-- wenn signatur gleich --> 
     xcbcvncvn 
     <!-- Datum --> 
     <xsl:if test="((DocumentSet/Document/Block/Field[@Type='5060']/Field[@Type='5064']/@Value != '') or (DocumentSet/Document/Block/Field[@Type='5060']/Field[@Type='5064']/@Value != $Halft/document/signatur/datum))"> 

     </xsl:if> 

     <!-- Ort --> 
     <xsl:if test="((DocumentSet/Document/Block/Field[@Type='ob26']/Field[@Type='2664']/@Value != '') or (DocumentSet/Document/Block/Field[@Type='ob26']/Field[@Type='2664']/@Value != $Halft/document/signatur/ort))"> 

     </xsl:if> 

     <!-- Ortsteil(Theater) --> 
     <xsl:if test="((DocumentSet/Document/Block/Field[@Type='ob26']/Field[@Type='2700']/@Value != '') or (DocumentSet/Document/Block/Field[@Type='ob26']/Field[@Type='2700']/@Value != $Halft/document/signatur/ortsteil))"> 

     </xsl:if> 

     <!-- Titel --> 
     <xsl:if test="((DocumentSet/Document/Block/Field[@Type='5200']/@Value != '') or (DocumentSet/Document/Block/Field[@Type='5200']/@Value != $Halft/document/signatur/titel))"> 
      <h1:Field Type="5200" Value="{$Halft/document/signatur/titel}"/> 
     </xsl:if> 

     <!-- Fotograf --> 
     <xsl:if test="((DocumentSet/Document/Block/Field[@Type='ob30']/Field[@Type='3100']/@Value != '') or (DocumentSet/Document/Block/Field[@Type='ob30']/Field[@Type='3100']/@Value != $Halft/document/signatur/fotograf))"> 

     </xsl:if> 

     <!-- Komponist --> 
     <xsl:if test="((DocumentSet/Document/Block/Field[@Type='5007']/Field[@Type='5009']/@Value != '') or (DocumentSet/Document/Block/Field[@Type='5007']/Field[@Type='5009']/@Value != $Halft/document/signatur/komponist))"> 

     </xsl:if> 
    </xsl:if> 
</xsl:template> 
+0

残念ながら、サンプル入力XMLとサンプルXSLコードはすぐには関係がありません.XSLにはXMLの何も一致しません。 [ヘルプセンター](https://stackoverflow.com/help)の[**最小限で完全で検証可能なサンプルを作成する方法**](https://stackoverflow.com/help/mcve)の記事をお読みください。 )それに応じてあなたの投稿を修正してください。 –

+0

アドバイスをいただきありがとうございますが、私は彼らがいかに即座の関係を示していないかを理解できないと言わなければなりません。私のxslの何もXMLの何にもマッチしないとどう思いますか?変数とif文で、両方のXMLファイルの要素の式がわかります。あなたのコメントを見た後、私の修正から、私は、単に例のxmlファイルで言及していないと言うことができます。それらは親要素 "DocumentSet"(複雑なもの)と "Document" ) 上に示しました。 –

+0

Re:一致しない: 'DocumentSet/Document'は何もマッチしません。なぜなら、1)サンプル入力XMLに' DocumentSet'要素がないので、 'Document'子は存在しません。 2)デフォルトの名前空間で 'Document'にマッチしたとしても、入力されたXMLはデフォルト以外の名前空間に' h1:Document'しかありません。ネームスペースと接頭辞は、XML処理の重要かつ難しい部分です。 XMLを使って多くのことを作業する場合は、これらの記事を読むのに役立つかもしれません。 –

答えて

0

次XSLT

<?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" 
    exclude-result-prefixes="xs" 
    version="2.0"> 

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

    <xsl:variable name="rework" select="document('simpleOne.xml')" /> 

    <!-- matching all documents that have a counterpart in 'simpleOne.xml'; others will be copied as is --> 
    <xsl:template match="*:Document[.//*:Field[@Type='2950']/@Value=$rework//*:signatur/@id]"> 
     <xsl:copy> 
      <xsl:copy-of select="@*"/> 
      <xsl:apply-templates> 
       <!-- handing over the current snippet/node from 'simpleOne.xml' --> 
       <xsl:with-param name="currentMergeInfo" select="$rework//*:signatur[@id = current()//*:Field[@Type='2950']/@Value]" as="node()" tunnel="yes"/> 
      </xsl:apply-templates> 
     </xsl:copy> 
    </xsl:template> 

    <!-- templates for actual replacement of the values --> 
    <xsl:template match="*:Field[@Type='3475']"> 
     <xsl:param name="currentMergeInfo" tunnel="yes"/> 
     <xsl:choose> 
      <xsl:when test="not($currentMergeInfo = '')"> 
       <xsl:copy> 
        <xsl:copy-of select="@* except @Value"/> 
        <!-- outputting the new value --> 
        <xsl:attribute name="Value"> 
         <xsl:value-of select="$currentMergeInfo//*:fotograf"/> 
        </xsl:attribute> 
       </xsl:copy>   
      </xsl:when> 
      <!-- if the parameter is an empty string just copy what is already there --> 
      <xsl:otherwise> 
       <xsl:copy-of select="."/> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 

</xsl:stylesheet> 

がにsimpleOne.xml

<?xml version="1.0" encoding="UTF-8"?> 
<signaturen> 
    <signatur id="TWS_FGL00541"> 
     <datum/> 
     <ort/> 
     <ortsteil/> 
     <titel>Fritz Feinhals als Wotan</titel> 
     <fotograf/> 
     <komponist>Wagner, Richard</komponist> 
     <author/> 
     <regie/> 
     <buehnenbild/> 
     <darPerson/> 
     <freiText>Walküre</freiText> 
    </signatur> 
    <signatur id="sampleID"> 
     <datum/> 
     <ort/> 
     <ortsteil/> 
     <titel>sample title</titel> 
     <fotograf>sample photographer</fotograf> 
     <komponist>sample composer</komponist> 
     <author/> 
     <regie/> 
     <buehnenbild/> 
     <darPerson/> 
     <freiText>sample text</freiText> 
    </signatur> 
</signaturen> 

に基づいて

<?xml version="1.0" encoding="UTF-8"?> 
<h1:DocumentSet xmlns:h1="someNamespace"> 
    <h1:Document DocKey="obj  40020528"> 
     <h1:Block Type="obj" 
      CreationDate="20.03.2014 09:39:50" 
      CreatorID="Admin" 
      ChangeDate="21.01.2015 14:40:51" 
      ChangerID="Admin" 
      OwnerID="Admin" 
      FieldsCount="17"> 
      <h1:Field Type="5000" Value="40020528"/> 
      <h1:Field Type="5060" Value="Aufnahme"> 
       <h1:Field Type="5064" Value="unbekannt"/> 
      </h1:Field> 
      <h1:Field Type="ob28" Value="Verwalter"> 
       <h1:Field Type="2864" Value="Köln"/> 
       <h1:Field Type="2900" 
        Value="Theaterwissenschaftliche Sammlung, Universität zu Köln"/> 
       <h1:Field Type="2930" Value="Fotoabteilung"/> 
       <h1:Field Type="2950" Value="TWS_FGL00541"/> 
       <h1:Field Type="907d" Value="No: 4260"/> 
      </h1:Field> 
      <h1:Field Type="ob26" Value="Aufnahmeort"> 
       <h1:Field Type="2664" Value="unbekannt"/> 
       <h1:Field Type="2690" Value="Theater"/> 
       <h1:Field Type="2700" Value="Theater unbekannt"/> 
      </h1:Field> 
      <h1:Field Type="ob30" Value="Herstellung"> 
       <h1:Field Type="3100" Value="unbekannt"/> 
       <h1:Field Type="3475" Value="Fotograf/in, Atelier"/> 
      </h1:Field> 
      <h1:Field Type="5007" Value="Darstellung"> 
       <h1:Field Type="5009" Value="unbekannt"/> 
       <h1:Field Type="5010" Value="Autor/in"/> 
       <h1:Field Type="5013" Value="xTITELx"/> 
      </h1:Field> 
      <h1:Field Type="ob40" Value="Inszenierung"> 
       <h1:Field Type="4100" Value="unbekannt"/> 
       <h1:Field Type="4475" Value="Regie"/> 
      </h1:Field> 
      <h1:Field Type="ob40" Value="Inszenierung"> 
       <h1:Field Type="4100" Value="unbekannt"/> 
       <h1:Field Type="4475" Value="Bühnenbild"/> 
      </h1:Field> 
      <h1:Field Type="5200" Value="Fritz Feinhals"/> 
      <h1:Field Type="5220" Value="Fotografie"/> 
      <h1:Field Type="5230" Value="Negativ"/> 
      <h1:Field Type="5240" Value="Glasplattennegativ"/> 
      <h1:Field Type="5360" Value="18x13"/> 
      <h1:Field Type="55th" Value="Feinhals, Fritz"/> 
     </h1:Block> 
    </h1:Document> 
    <h1:Document DocKey="obj  40020528"> 
     <h1:Block Type="obj" 
      CreationDate="20.03.2014 09:39:50" 
      CreatorID="Admin" 
      ChangeDate="21.01.2015 14:40:51" 
      ChangerID="Admin" 
      OwnerID="Admin" 
      FieldsCount="17"> 
      <h1:Field Type="5000" Value="someValue"/> 
      <h1:Field Type="5060" Value="Aufnahme"> 
       <h1:Field Type="5064" Value="unbekannt"/> 
      </h1:Field> 
      <h1:Field Type="ob28" Value="Verwalter"> 
       <h1:Field Type="2864" Value="somePlace"/> 
       <h1:Field Type="2900" 
        Value="Theaterwissenschaftliche Sammlung, Universität zu Köln"/> 
       <h1:Field Type="2930" Value="Fotoabteilung"/> 
       <h1:Field Type="2950" Value="sampleID"/> 
       <h1:Field Type="907d" Value="someNumber"/> 
      </h1:Field> 
      <h1:Field Type="ob26" Value="Aufnahmeort"> 
       <h1:Field Type="2664" Value="unbekannt"/> 
       <h1:Field Type="2690" Value="someEntity"/> 
       <h1:Field Type="2700" Value="someEntity"/> 
      </h1:Field> 
      <h1:Field Type="ob30" Value="Herstellung"> 
       <h1:Field Type="3100" Value="unbekannt"/> 
       <h1:Field Type="3475" Value="Fotograf/in, Atelier"/> 
      </h1:Field> 
      <h1:Field Type="5007" Value="Darstellung"> 
       <h1:Field Type="5009" Value="unbekannt"/> 
       <h1:Field Type="5010" Value="Autor/in"/> 
       <h1:Field Type="5013" Value="xTITELx"/> 
      </h1:Field> 
      <h1:Field Type="ob40" Value="Inszenierung"> 
       <h1:Field Type="4100" Value="unbekannt"/> 
       <h1:Field Type="4475" Value="Regie"/> 
      </h1:Field> 
      <h1:Field Type="ob40" Value="Inszenierung"> 
       <h1:Field Type="4100" Value="unbekannt"/> 
       <h1:Field Type="4475" Value="Bühnenbild"/> 
      </h1:Field> 
      <h1:Field Type="5200" Value="someName"/> 
      <h1:Field Type="5220" Value="someType"/> 
      <h1:Field Type="5230" Value="someFormat"/> 
      <h1:Field Type="5240" Value="someSpecification"/> 
      <h1:Field Type="5360" Value="someFormat"/> 
      <h1:Field Type="55th" Value="someName"/> 
     </h1:Block> 
    </h1:Document> 
</h1:DocumentSet> 

をあなたの(やや拡張)入力を変換します私は簡単に新しい値にこれをマップすることができように、出力

<?xml version="1.0" encoding="UTF-8"?> 
<h1:DocumentSet xmlns:h1="someNamespace"> 
    <h1:Document DocKey="obj  40020528"> 
     <h1:Block Type="obj" CreationDate="20.03.2014 09:39:50" CreatorID="Admin" ChangeDate="21.01.2015 14:40:51" ChangerID="Admin" OwnerID="Admin" FieldsCount="17"> 
      <h1:Field Type="5000" Value="40020528"/> 
      <h1:Field Type="5060" Value="Aufnahme"> 
       <h1:Field Type="5064" Value="unbekannt"/> 
      </h1:Field> 
      <h1:Field Type="ob28" Value="Verwalter"> 
       <h1:Field Type="2864" Value="Köln"/> 
       <h1:Field Type="2900" Value="Theaterwissenschaftliche Sammlung, Universität zu Köln"/> 
       <h1:Field Type="2930" Value="Fotoabteilung"/> 
       <h1:Field Type="2950" Value="TWS_FGL00541"/> 
       <h1:Field Type="907d" Value="No: 4260"/> 
      </h1:Field> 
      <h1:Field Type="ob26" Value="Aufnahmeort"> 
       <h1:Field Type="2664" Value="unbekannt"/> 
       <h1:Field Type="2690" Value="Theater"/> 
       <h1:Field Type="2700" Value="Theater unbekannt"/> 
      </h1:Field> 
      <h1:Field Type="ob30" Value="Herstellung"> 
       <h1:Field Type="3100" Value="unbekannt"/> 
       <h1:Field Type="3475" Value="Max Muster"/> 
      </h1:Field> 
      <h1:Field Type="5007" Value="Darstellung"> 
       <h1:Field Type="5009" Value="unbekannt"/> 
       <h1:Field Type="5010" Value="Autor/in"/> 
       <h1:Field Type="5013" Value="xTITELx"/> 
      </h1:Field> 
      <h1:Field Type="ob40" Value="Inszenierung"> 
       <h1:Field Type="4100" Value="unbekannt"/> 
       <h1:Field Type="4475" Value="Regie"/> 
      </h1:Field> 
      <h1:Field Type="ob40" Value="Inszenierung"> 
       <h1:Field Type="4100" Value="unbekannt"/> 
       <h1:Field Type="4475" Value="Bühnenbild"/> 
      </h1:Field> 
      <h1:Field Type="5200" Value="Fritz Feinhals"/> 
      <h1:Field Type="5220" Value="Fotografie"/> 
      <h1:Field Type="5230" Value="Negativ"/> 
      <h1:Field Type="5240" Value="Glasplattennegativ"/> 
      <h1:Field Type="5360" Value="18x13"/> 
      <h1:Field Type="55th" Value="Feinhals, Fritz"/> 
     </h1:Block> 
    </h1:Document> 
    <h1:Document DocKey="obj  40020528"> 
     <h1:Block Type="obj" CreationDate="20.03.2014 09:39:50" CreatorID="Admin" ChangeDate="21.01.2015 14:40:51" ChangerID="Admin" OwnerID="Admin" FieldsCount="17"> 
      <h1:Field Type="5000" Value="someValue"/> 
      <h1:Field Type="5060" Value="Aufnahme"> 
       <h1:Field Type="5064" Value="unbekannt"/> 
      </h1:Field> 
      <h1:Field Type="ob28" Value="Verwalter"> 
       <h1:Field Type="2864" Value="somePlace"/> 
       <h1:Field Type="2900" Value="Theaterwissenschaftliche Sammlung, Universität zu Köln"/> 
       <h1:Field Type="2930" Value="Fotoabteilung"/> 
       <h1:Field Type="2950" Value="sampleID"/> 
       <h1:Field Type="907d" Value="someNumber"/> 
      </h1:Field> 
      <h1:Field Type="ob26" Value="Aufnahmeort"> 
       <h1:Field Type="2664" Value="unbekannt"/> 
       <h1:Field Type="2690" Value="someEntity"/> 
       <h1:Field Type="2700" Value="someEntity"/> 
      </h1:Field> 
      <h1:Field Type="ob30" Value="Herstellung"> 
       <h1:Field Type="3100" Value="unbekannt"/> 
       <h1:Field Type="3475" Value="sample photographer"/> 
      </h1:Field> 
      <h1:Field Type="5007" Value="Darstellung"> 
       <h1:Field Type="5009" Value="unbekannt"/> 
       <h1:Field Type="5010" Value="Autor/in"/> 
       <h1:Field Type="5013" Value="xTITELx"/> 
      </h1:Field> 
      <h1:Field Type="ob40" Value="Inszenierung"> 
       <h1:Field Type="4100" Value="unbekannt"/> 
       <h1:Field Type="4475" Value="Regie"/> 
      </h1:Field> 
      <h1:Field Type="ob40" Value="Inszenierung"> 
       <h1:Field Type="4100" Value="unbekannt"/> 
       <h1:Field Type="4475" Value="Bühnenbild"/> 
      </h1:Field> 
      <h1:Field Type="5200" Value="someName"/> 
      <h1:Field Type="5220" Value="someType"/> 
      <h1:Field Type="5230" Value="someFormat"/> 
      <h1:Field Type="5240" Value="someSpecification"/> 
      <h1:Field Type="5360" Value="someFormat"/> 
      <h1:Field Type="55th" Value="someName"/> 
     </h1:Block> 
    </h1:Document> 
</h1:DocumentSet> 

はこれまでのところ、この変換はのみ@type="3475"でフィールドを気に。考え方は、他の子供のための類似のテンプレートを作成することですsignatur


:私はあなたが一度に複数の「ドキュメント」を処理する必要がある場合にはh1:DocumentSetで2つのh1:Documentのノードを包みました。同様に、私はsimpleOne.xmlにラッパー要素を作成しました。ファイルを1つずつ処理する場合は、これを簡単に削除する必要があります。また、ワイルドカードを使用して名前空間を保護しました。これは間違いなくベストプラクティスではありません。正しく使用するようにしてください。

+0

ありがとうございます! –

+0

@EnesteziAbdelQader:重要な注意点:user9775のコードでは、サンプル入力XMLに接頭辞と名前空間情報が存在しないことを回避するために、XSL 2.0以上のテクニックを使用しています。このテクニックは、1)あなたのXSLプロセッサがXSL 2.0+をサポートしていて、2)すべての 'Document'、' Field'などの要素が1つの名前空間だけにある場合(つまり、接頭辞が同じであればあなたのサンプル入力から 'h1')。 'h1'接頭辞を持つ' Document'要素と、他の接頭辞を持つ 'Document'要素がある場合、このコードは期待される出力を生成しません。 –

関連する問題