2017-06-14 61 views
0

我有一個相當複雜的XML文件,一個更簡單,其中包含一些更復雜的文件。從複雜的 XSLT - 比較和組合兩個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> 

我想每個「簽字:」這兩個文件的內容進行比較。如果簽名相同,則會進行更正。如果存在更正,則必須檢查來自簡單XML的哪些子元素必須放入複雜子元素的子元素中,因此必須檢查簡單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/mcve)文章(https://stackoverflow.com/help )並相應地修改你的帖子。 –

+0

謝謝你的建議,但我不得不說,我不能理解他們如何不直接顯示關係。你怎麼能不假設我的xsl會匹配我的XML中的任何東西?你可以在變量和if語句中看到我擁有這兩個XML文件中元素的表達式。從我看到你的評論後的修改,我只能說,我只是沒有提到在示例XML文件,他們有複雜的父母元素「DocumentSet」和簡單的一個,其中包含對象(或元素) 如上所示。 –

+0

回覆:不匹配:'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> 

將您(略擴展)輸入

<?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> 

基於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="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> 

到目前爲止,這種轉變只關心字段01​​,因爲我可以很容易地映射到這個新的價值。這個想法是爲signatur的其他孩子創建類似的模板。


注意:我裹在h1:DocumentSet 2個h1:Document節點的情況下,你需要處理一些「文件」一次。同樣,我在simpleOne.xml中創建了一個包裝元素。如果逐個處理文件,應該很容易刪除它。另外,我使用通配符來簡化名稱空間 - 這絕對不是最佳實踐,請嘗試正確使用它們。

+0

非常感謝你! –

+0

@EnesteziAbdelQader:重要提醒:user9775的代碼使用'*:Document'的XSL 2.0+技術來解決樣本輸入XML中缺少前綴和名稱空間信息的問題。如果1)您的XSL處理器支持XSL 2.0+,並且2)您的所有'Document','Field'等等元素僅在一個名稱空間中找到(即所有具有相同前綴,假設這將是來自樣本輸入的'h1')。如果你有一些帶'h1'前綴的'Document'元素和其他帶有任何其他前綴的'Document'元素,這段代碼將不會產生預期的輸出。 –