2015-11-04 43 views
0

我有一個Excel工作表以汽車列表:如何使用XML模式將多個Excel行導出到XML文件?

Row 1 contains column descriptors for vehicle attributes

我創建了一個XML架構文檔,讓我的工作表導出到XML。該架構如下:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="Root"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <!--Below are the primary vehicle descriptors - essentially the attributes for the cars--> 
       <xsd:element name="Brand" type="xsd:string"/> 
       <xsd:element name="Model" type="xsd:string"/> 
       <xsd:element name="Colour" type="xsd:string"/> 
       <xsd:element name="Price" type="xsd:string"/> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 
</xsd:schema> 

在Excel中,我點擊開發>源> XML映射>添加,然後我從上面選擇的XML模式文件,並將其添加到我的工作表。 我將XML Source窗口中的每個元素映射到Excel工作表中的相應列(「品牌」元素映射到A:A,「模型」元素映射到B:B等)。

enter image description here

當它來導出映射表(使用開發者>導出,然後選擇一個目錄和文件名),生成的XML文件,只有從工作表數據的第一行。由於我的XML映射包含的工作表的標題行中,XML文件是這樣的:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<Root> 
    <Brand>Brand</Brand> 
    <Model>Model</Model> 
    <Colour>Colour</Colour> 
    <Price>Price</Price> 
</Root> 

我已經通過多種不同的幫助頁面一看,我不知道如何讓我的XML文件存儲每Excel工作表的行(全部五個,包括標題行)。

回答

1

我沒有使用J.Doe的答案,但我認爲這是正確的,因爲它的相似,我終於實現了。鏈接原始XML模式時:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="Root"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <!--Below are the primary vehicle descriptors - essentially the attributes for the cars--> 
       <xsd:element name="Brand" type="xsd:string"/> 
       <xsd:element name="Model" type="xsd:string"/> 
       <xsd:element name="Colour" type="xsd:string"/> 
       <xsd:element name="Price" type="xsd:string"/> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 
</xsd:schema> 

Excel假定表格中只有一條記錄。解決方案是在我的模式中創建第二個複雜元素(除了名爲「Root」的元素),其中包含> 1個我的根元素。得到的文件是這樣的:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="Car_Table"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element ref="Root" minOccurs="0" maxOccurs="unbounded"/> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 
    <xsd:element name="Root"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <!--Below are the primary vehicle descriptors - essentially the attributes for the cars--> 
       <xsd:element name="Brand" type="xsd:string"/> 
       <xsd:element name="Model" type="xsd:string"/> 
       <xsd:element name="Colour" type="xsd:string"/> 
       <xsd:element name="Price" type="xsd:string"/> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 
</xsd:schema> 

當使用開發者>源> XML映射>添加,你會得到這樣的提示添加模式:

Window showing the root selection

選擇它引用的元素其他元素(因爲這允許遞歸)。在XML源面板,地圖將看起來像這樣:

The Schema source which will allow for recursive exporting to an XML file

然後可以映射在架構以在片材中的相應列的每個元素(品牌,型號,顏色,價格)。一旦方案被映射到表,您的數據將是這樣的:

The data in the sheet after successfully mapping the schema

然後,您可以去開發>導出數據導出到XML。選擇一個文件名,並使用導出對話框目錄後,生成的XML文件應該是這個樣子:

Data from the Cars sheet in XML format

這是使用Excel 2010中完成的,其他版本也將有所不同。

2

您應該添加什麼地方這樣的:

minOccurs="0" maxOccurs="unbounded" 

告訴脫穎而出,你的元素可以出現多次。

但是,您不能將其添加到「根」元素中。

試試這個:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="Root"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element name="Car" minOccurs="0" maxOccurs="unbounded"> 
        <xsd:complexType> 
         <xsd:sequence> 
          <xsd:element name="Brand" type="xsd:string"/> 
          <xsd:element name="Model" type="xsd:string"/> 
          <xsd:element name="Colour" type="xsd:string"/> 
          <xsd:element name="Price" type="xsd:string"/> 
         </xsd:sequence> 
        </xsd:complexType> 
       </xsd:element> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 
</xsd:schema> 
+0

工作完美! – Albert

1

對我而言,簡單地編輯架構併爲每個實體都有多個條目。所以在你的情況下:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="Root"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <!--Below are the primary vehicle descriptors - essentially the attributes for the cars--> 
       <xsd:element name="Brand" type="xsd:string"/> 
       <xsd:element name="Brand" type="xsd:string"/> 
       <xsd:element name="Model" type="xsd:string"/> 
       <xsd:element name="Model" type="xsd:string"/> 
       <xsd:element name="Colour" type="xsd:string"/> 
       <xsd:element name="Colour" type="xsd:string"/> 
       <xsd:element name="Price" type="xsd:string"/> 
       <xsd:element name="Price" type="xsd:string"/> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 
</xsd:schema> 

只需要爲每個條目放入兩個條目,這允許導出多個條目。

+0

簡單,優雅,它實際上適用於我..謝謝@MapZombie – Gravinco