XSLT:如何在XML文档中的特定位置插入已编辑的元素

时间:2016-09-16 11:58:04

标签: xml xml-parsing xslt-1.0 xslt-2.0

我试图在XML文档中进行更改,而我在插入一组元素(在XSLT中进行了轻微编辑)时遇到了问题。所以我们走了,这是XML:

`<?xml version="1.0" encoding="UTF-8"?>
<h1:DocumentSet xmlns:h1="http://www.startext.de/HiDA/DefService/XMLSchema">
  <h1:ContentInfo>
    <h1:Format>HIDA-DOC1-XML</h1:Format>
    <h1:CreationDate>21.04.2016 14:17:30:042</h1:CreationDate>
  </h1:ContentInfo> 
<h1:Document DocKey="obj     00000003" FieldsCount="286">
    <h1:Block Type="obj" CreationDate="03.02.2014 11:43:26" CreatorID="Admin" ChangeDate="21.01.2015 10:29:20" ChangerID="Admin" OwnerID="Admin" FieldsCount="220">
      <h1:Field Type="5000" Value="00000003"/>
      <h1:Field Type="9920" Value="1931.03.07"/>
      <h1:Field Type="ob28" Value="TWS">
        <h1:Field Type="2800"/>
        <h1:Field Type="2864" Value="München"/>
        <h1:Field Type="2862" Value="unbekannt"/>
        <h1:Field Type="2930" Value="Fotoabteilung"/>
        <h1:Field Type="2950" Value="TWS_FGL00001"/>
        <h1:Field Type="907d" Value="No. 231"/>
      </h1:Field>
      <h1:Field Type="5108" Value="München">
        <h1:Field Type="5110" Value="Theater unbekannt"/>
      </h1:Field>
      <h1:Field Type="ob30">
        <h1:Field Type="3100" Value="unbekannt"/>
        <h1:Field Type="3475" Value="Fotograf/in, Atelier"/>
      </h1:Field>
      <h1:Field Type="ob30">
        <h1:Field Type="3100" Value="unbekannt"/>
        <h1:Field Type="3475" Value="Verlag"/>
      </h1:Field>
      <h1:Field Type="ob30">
        <h1:Field Type="3100" Value="Grillparzer, Franz"/>
        <h1:Field Type="3475" Value="Autor/in"/>
      </h1:Field>
      <h1:Field Type="ob30">
        <h1:Field Type="3100" Value="Böhm, Karl Hans"/>
        <h1:Field Type="3475" Value="Regie"/>
      </h1:Field>
      <h1:Field Type="ob30">
        <h1:Field Type="3100" Value="Linnebach, Adolf"/>
        <h1:Field Type="3475" Value="Bühnenbild"/>
      </h1:Field>
      <h1:Field Type="5200" Value="Ahnfrau, Die"/>
      <h1:Field Type="5220" Value="Fotografie"/>
      <h1:Field Type="5226" Value="Negativ"/>
      <h1:Field Type="5230" Value="Glasplattennegativ"/>
      <h1:Field Type="agZeit"/>
      <h1:Field Type="agThema"/>
      <h1:Field Type="5360" Value="18x13"/>
      <h1:Field Type="55th" Value="Zäpfel, Armand"/>
      <h1:Field Type="8450">
        <h1:Field Type="8470"/>
        <h1:Field Type="8540" Value="F:\Glasplattennegative\JPEG\TWS_FGL00001"/>
        <h1:Field Type="8555" Value="F:\Glasplattennegative\JPEG\TWS_FGL_00001"/>
        <h1:Field Type="8555mf" Value="F:\Glasplattennegative\JPEG\TWS_FGL_00001"/>
        <h1:Field Type="8e01" Value="TWS_FGL00001.jpg"/>
        <h1:Field Type="8e02" Value="3347"/>
        <h1:Field Type="8e03" Value="4598"/>
        <h1:Field Type="8e04" Value="24"/>
        <h1:Field Type="8e05" Value="355049"/>
        <h1:Field Type="8e06" Value="Perfection V700/V750"/>
        <h1:Field Type="8e07" Value="EPSON"/>
        <h1:Field Type="8e08" Value="2014:02:06 14:16:13"/>
      </h1:Field>
      <h1:Field Type="9990" Value="Fritz Basil als Graf von Borotin&#x3b; Midi Scheinpflug als Berta, seine Tochter&#x3b; Armand Zäpfel als Jaromir&#x3b; Wolf von Strobl als Boleslav&#x3b; Arnulf Schröder als Günther, Kastellan&#x3b; Albert Lippert als Ein Hauptmann&#x3b; Albert Wisheu als Ein Soldat"/>
    </h1:Block>
</h1:Document>
</h1:DocumentSet>`

这是我的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"
    xmlns:h1="http://www.startext.de/HiDA/DefService/XMLSchema"
    xpath-default-namespace="http://www.startext.de/HiDA/DefService/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match=".//Field[@Type='9920']">
        <h1:Field Type="5060" Value="Aufnahme">
            <h1:Field Type="5064" Value="{@Value}"/>
        </h1:Field>       
    </xsl:template>

    <xsl:template match=".//Field[@Type='ob28']">
        <h1:Field Type="ob28" Value="Verwalter">
            <h1:Field Type="2864" Value="Köln"/>
            <h1:Field Type="2900" Value="{@Value}"/>
            <xsl:apply-templates select=".//Field[@Type='2930']"/>
            <xsl:apply-templates select=".//Field[@Type='2950']"/>
            <xsl:apply-templates select=".//Field[@Type='907d']"/>
        </h1:Field>
    </xsl:template>

    <xsl:template match=".//Field[@Type='5108']">
        <h1:Field Type="ob26" Value="Aufnahmeort">
            <h1:Field Type="2664" Value="{@Value}"/>
            <h1:Field Type="2690" Value="Theater"/>
            <h1:Field Type="2700" Value="{.//Field/@Value}"/>
        </h1:Field>
    </xsl:template>

    <xsl:template match=".//Field[@Type='ob30']"/>

    <!--   To Do: here is my problem! i want it to appear exact same position, where the first ob30 in xml starts
    <xsl:template match=".//Block">
            <xsl:call-template name="alles"/>
            <xsl:for-each select=".//Field[@Type='ob30']">
                <xsl:if test="position()= 1">
                <h1:Field Type="ob30" Value="Herstellung">
                  <xsl:apply-templates select=".//Field[@Type='3100']"/>
                  <xsl:apply-templates select=".//Field[@Type='3475']"/>
                </h1:Field>
                </xsl:if>
                <xsl:if test="position()= 3">
                    <h1:Field Type="ob30" Value="Darstellung">
                        <xsl:apply-templates select=".//Field[@Type='3100']"/>
                        <xsl:apply-templates select=".//Field[@Type='3475']"/>
                        <h1:Field Type="5013" Value="xTITELx"/>
                    </h1:Field>
                </xsl:if>
                <xsl:if test="position()= 4 or position()= 5">
                    <h1:Field Type="ob30" Value="Inszenierung">
                        <xsl:apply-templates select=".//Field[@Type='3100']"/>
                        <xsl:apply-templates select=".//Field[@Type='3475']"/>
                    </h1:Field>
                </xsl:if>
            </xsl:for-each>
     </xsl:template>
    -->

    <xsl:template match=".//Field[@Type='5226']">
            <h1:Field Type="5230" Value="{@Value}"/>
    </xsl:template>

    <xsl:template match=".//Field[@Type='5230']">
        <h1:Field Type="5240" Value="{@Value}"/>
    </xsl:template>

    <xsl:template match=".//Field[@Type='9990']">
        <h1:Field Type="599a" Value="Werkkommentar">
            <h1:Field Type="599e" Value="{@Value}"/>
            <h1:Field Type="599n" Value="P,N"/>
        </h1:Field>
        <h1:Field Type="9902" Value="TWS"/>
        <h1:Field Type="9904" Value="P,N"/>
    </xsl:template>
</xsl:stylesheet>

我希望编辑过的元素具有属性&#34; ob30&#34;显示完全相同的位置,xml中的第一个ob30开始。具有ob30属性的元素显示为&#34; Block&#34;的兄弟元素。元件。我不知道如何解决它。你有一个实用的解决方案吗?

编辑:在这里,我添加了所需的输出:

    <?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:h1="http://www.startext.de/HiDA/DefService/XMLSchema"
  xpath-default-namespace="http://www.startext.de/HiDA/DefService/XMLSchema"
  exclude-result-prefixes="xs"
  version="2.0">
  <xsl:output method="xml" encoding="utf-8" />

 <h1:DocumentSet xmlns:h1="http://www.startext.de/HiDA/DefService/XMLSchema">
  <h1:ContentInfo>
    <h1:Format>HIDA-DOC1-XML</h1:Format>
    <h1:CreationDate>09.06.2015 09:31:15:408</h1:CreationDate>
  </h1:ContentInfo>
  <h1:Document DocKey="obj     40020000">
    <h1:Block Type="obj" CreationDate="03.02.2014 11:43:26" CreatorID="Admin" ChangeDate="07.02.2014 13:29:02" ChangerID="Admin" OwnerID="Admin">
      <h1:Field Type="5000" Value="40020000"/>
      <h1:Field Type="5060" Value="Aufnahme">
        <h1:Field Type="5064" Value="1931.03.07"/>
      </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="2940" Value="München"/>
        <h1:Field Type="2950" Value="TWS_FGL00001"/>
        <h1:Field Type="907d" Value="No. 231"/>
      </h1:Field>
      <h1:Field Type="ob26" Value="Aufnahmeort">
        <h1:Field Type="2664" Value="München"/>
        <h1:Field Type="2690" Value="Theater"/>
        <h1:Field Type="2700" Value="unbekanntes Theater"/>
      </h1:Field>
            <h1:Field Type="ob30" Value="Herstellung">
        <h1:Field Type="3100" Value="unbekannter Fotograf"/>
        <h1:Field Type="3475" Value="Fotograf"/>
      </h1:Field>
      <h1:Field Type="5007" Value="Darstellung">
        <h1:Field Type="5009" Value="Grillparzer, Franz"/>
        <h1:Field Type="5010" Value="Schauspiel"/>
        <h1:Field Type="5013" Value="xTITELx"/>
      </h1:Field>
      <h1:Field Type="ob40" Value="Inszenierung">
        <h1:Field Type="4100" Value="Böhm, Karl Hans"/>
        <h1:Field Type="4475" Value="Regisseur"/>
      </h1:Field>
      <h1:Field Type="ob40" Value="Inszenierung">
        <h1:Field Type="4100" Value="Linnebach, Adolf"/>
        <h1:Field Type="4475" Value="Bühnenbildner"/>
      </h1:Field>
      <h1:Field Type="5200" Value="Die Ahnfrau"/>
      <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="unbekannt"/>
      <h1:Field Type="8450" Value="digitale Reproduktion">
        <h1:Field Type="8540" Value="TWS_FGL00001"/>
         <h1:Field Type="8494" Value="1931.03.07"/>
        <h1:Field Type="8555" Value="F:\Glasplattennegative\JPEG\TWS_FGL_00001"/>
        <h1:Field Type="8555mf" Value="F:\Glasplattennegative\JPEG\TWS_FGL_00001"/>
        <h1:Field Type="8e01" Value="TWS_FGL00001.jpg"/>
        <h1:Field Type="8e02" Value="3347"/>
        <h1:Field Type="8e03" Value="4598"/>
        <h1:Field Type="8e04" Value="24"/>
        <h1:Field Type="8e05" Value="355049"/>
        <h1:Field Type="8e06" Value="Perfection V700/V750"/>
        <h1:Field Type="8e07" Value="EPSON"/>
        <h1:Field Type="8e08" Value="2014:02:06 14:16:13"/>
      </h1:Field>
      <h1:Field Type="599a" Value="Werkkommentar">
        <h1:Field Type="599e" Value="Fritz Basil als Graf von Borotin&#x3b; Midi Scheinpflug als Berta, seine Tochter&#x3b; Armand Zäpfel als Jaromir&#x3b; Wolf von Strobl als Boleslav&#x3b; Arnulf Schröder als Günther, Kastellan&#x3b; Albert Lippert als Ein Hauptmann&#x3b; Albert Wisheu als Ein Soldat"/>
        <h1:Field Type="599n" Value="P,N"/>
        </h1:Field>
          <h1:Field Type="9902" Value="TWS"/>
      <h1:Field Type="9904" Value="P,N"/>
    </h1:Block>
</h1:Document>
</h1:DocumentSet>

1 个答案:

答案 0 :(得分:0)

尝试以下几行:

<xsl:template match="Field[@Type='ob30']"/>

<xsl:template match="Field[@Type='ob30'][1]" priority="5">
    <h1:Field Type="ob30" Value="Herstellung">
        <xsl:apply-templates select=".//Field[@Type='3100']"/>
        <xsl:apply-templates select=".//Field[@Type='3475']"/>
    </h1:Field>
</xsl:template>

<xsl:template match="Field[@Type='ob30'][3]" priority="5">
    <h1:Field Type="ob30" Value="Darstellung">
        <xsl:apply-templates select=".//Field[@Type='3100']"/>
        <xsl:apply-templates select=".//Field[@Type='3475']"/>
        <h1:Field Type="5013" Value="xTITELx"/>
    </h1:Field>
</xsl:template>

<xsl:template match="Field[@Type='ob30'][position() = (4, 5)]" priority="5">
    <h1:Field Type="ob30" Value="Inszenierung">
        <xsl:apply-templates select=".//Field[@Type='3100']"/>
        <xsl:apply-templates select=".//Field[@Type='3475']"/>
    </h1:Field>
</xsl:template>