2014-03-06 152 views
-3

我必須通過XSL將XML轉換爲另一個XML。我有以下XML從XML創建用於XML的XSLT

<?xml version="1.0" encoding="UTF-8"?><SKOS xmlns="http://www.w3.org/2004/02/skos/core#" xmlns:skos="http://www.w3.org/2004/02/skos/core#" xmlns:skosxl="http://www.w3.org/2008/05/skos-xl#"> 
    <skos:descriptor ID="7769" TAGNO="111" ISVALID="true"> 
     <skos:altLabel>ABYSSINIAN Expedition (1867-1868)</skos:altLabel> 
     <skos:prefLabel>Abyssinian Expedition (1867-1868)</skos:prefLabel> 
     <skos:broader>Ethiopia -- History -- 1490-1889</skos:broader> 
     <skos:broader>Great Britain -- History -- Victoria, 1837-1901</skos:broader> 
     <skosxl:hiddenLabel>Expedition to Abyssinia (1867-1868)</skosxl:hiddenLabel> 
     <skosxl:hiddenLabel>British Expedition to Abyssinia (1867-1868)</skosxl:hiddenLabel> 
     <skosxl:hiddenLabel>Magdala Campaign, 1867-1868</skosxl:hiddenLabel> 
     <skosxl:hiddenLabel>Napier Expedition, 1867-1868</skosxl:hiddenLabel> 
    </skos:descriptor> 
</SKOS> 

,我希望輸出這樣

<add> 
<doc> 
    <field name="id">7769</field> 
    <field name="TAGNO">111</field> 
    <field name="altLabel">ABYSSINIAN Expedition (1867-1868)</field> 
    <field name="broader">Ethiopia -- History -- 1490-1889</field> 
    <field name="broader">Great Britain -- History -- Victoria, 1837-1901</field> 
    <field name="hiddenLabel">Expedition to Abyssinia (1867-1868)</field> 
    <field name="hiddenLabel">British Expedition to Abyssinia (1867-1868)</field> 
    <field name="hiddenLabel">Magdala Campaign, 1867-1868</field> 
    <field name="hiddenLabel">Napier Expedition, 1867-1868</field> 

</doc> 

我沒有得到如何利用它XSLT。

回答

0

你沒有提供關於如何在輸出輸入結果(如爲什麼會出現在輸出中沒有ISVALID場?是有關這個過程ISVALID還是應該被忽略?爲什麼沒有prefLabel場輸出解釋?),但是這會產生你所描述的輸出:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:skos="http://www.w3.org/2004/02/skos/core#" 
       exclude-result-prefixes="skos" 
       > 
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 

    <xsl:template match="/*"> 
    <add> 
     <xsl:apply-templates select="skos:descriptor" /> 
    </add> 
    </xsl:template> 

    <xsl:template match="skos:descriptor"> 
    <doc> 
     <field name="id"> 
     <xsl:value-of select="@ID"/> 
     </field> 
     <xsl:apply-templates select="@*[not(name() = 'ID' or name() = 'ISVALID')] | 
            *[not(self::skos:prefLabel)]" /> 
    </doc> 
    </xsl:template> 

    <xsl:template match="@* | *"> 
    <field name="{local-name()}"> 
     <xsl:value-of select="." /> 
    </field> 
    </xsl:template> 
</xsl:stylesheet> 

當你的樣品輸入運行,其結果是:

<add> 
    <doc> 
    <field name="id">7769</field> 
    <field name="TAGNO">111</field> 
    <field name="altLabel">ABYSSINIAN Expedition (1867-1868)</field> 
    <field name="broader">Ethiopia -- History -- 1490-1889</field> 
    <field name="broader">Great Britain -- History -- Victoria, 1837-1901</field> 
    <field name="hiddenLabel">Expedition to Abyssinia (1867-1868)</field> 
    <field name="hiddenLabel">British Expedition to Abyssinia (1867-1868)</field> 
    <field name="hiddenLabel">Magdala Campaign, 1867-1868</field> 
    <field name="hiddenLabel">Napier Expedition, 1867-1868</field> 
    </doc> 
</add> 
+0

它藉機d。非常感謝。 – user

0
  String st = "Your Xml"; 
      XmlDocument doc = new XmlDocument(); 
      doc.LoadXml(st); 
      XmlReader read = XmlReader.Create(new StringReader(st)); 
      string Xml = "<doc>"; 
      while (read.Read()) 
      { 
       if (read.AttributeCount > 0) 
       { 
        for (int i = 0; i < read.AttributeCount; i++) 
        { 
         if (read.Name == "xml" || read.Name.Split(':').Count() < 2) 
          break; 
         var node = doc.GetElementsByTagName(read.Name); 
         Xml += "<field name=\"" + node[0].Attributes[i].Name + "\">" + node[0].Attributes[i].Value + "</field>"; 
        } 
       } 
       else 
       { 
        if (read.Name != string.Empty && read.Name.Split(':').Count() > 1) 
         Xml += "<field name=\"" + read.LocalName + "\">" + read.ReadInnerXml() + "</field>"; 
       } 
      } 
      Xml += "</doc>";