将属性转换为元素

时间:2015-02-02 15:36:34

标签: xml xslt ssis

您好我有以下XML,

<?xml version = "1.0" encoding = "utf-8"?>
<root>
<document> 
    <field   level = "document" name = "Client Number" value = "00000300688"/>
    <field   level = "document" name = "Client Short Name" value = "SOME CLIENT"/>
    <field   level = "document" name = "IBS Oper Acct ACBS Cust Num" value = "00004437252"/>
    <field   level = "document" name = "Document Type" value = "LaserPro Disbursement Request form"/>
    <field   level = "document" name = "Effective Date" value = "02-13-2014"/>
</document> 
</root>

我需要像这样格式化:

<?xml version = "1.0" encoding = "utf-8"?>
<root>
<document>
    <ClientNumber>00000300688</ClientNumber>
    <ClientShortName>SOME CLIENT</ClientShortName>
    <IBSOperAcctACBSCustNum>00004437252</IBSOperAcctACBSCustNum>
    <DocumentType>LaserPro Disbursement Request form</DocumentType>
    <EffectiveDate>02-13-2014</EffectiveDate>
</document> 
</root>

所以,基本上我需要将我的属性转换为元素(当然要删除空格,因为我认为元素不能包含空格)。

这样做的问题是我有超过30,000个XML文件需要像这样转换。然后我使用SSIS将此数据导入SQL Server中的表。基本上我的.xml文件中的每一个都是目标表中的一行。我希望我的XML属性成为该表中的列,并将该属性的值作为行数据。

我对XML很新,所以任何提示都是有帮助的!非常感谢!

1 个答案:

答案 0 :(得分:1)

此XSLT转换:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

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

  <xsl:template match="field">
    <xsl:element name="{translate(@name, ' ', '')}">
      <xsl:value-of select="@value"/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

将采用您给定的XML:

<?xml version = "1.0" encoding = "utf-8"?>
<root>
<document> 
    <field   level = "document" name = "Client Number" value = "00000300688"/>
    <field   level = "document" name = "Client Short Name" value = "SOME CLIENT"/>
    <field   level = "document" name = "IBS Oper Acct ACBS Cust Num" value = "00004437252"/>
    <field   level = "document" name = "Document Type" value = "LaserPro Disbursement Request form"/>
    <field   level = "document" name = "Effective Date" value = "02-13-2014"/>
</document> 
</root>

并提供以下XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <document> 
      <ClientNumber>00000300688</ClientNumber>
      <ClientShortName>SOME CLIENT</ClientShortName>
      <IBSOperAcctACBSCustNum>00004437252</IBSOperAcctACBSCustNum>
      <DocumentType>LaserPro Disbursement Request form</DocumentType>
      <EffectiveDate>02-13-2014</EffectiveDate>
   </document> 
</root>

根据要求。