将属性转换为元素

Convert attributes to elements

你好我有以下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 服务器中的 table。基本上我的每个 .xml 文件在目标 table 中都是一行。我希望我的 XML 属性成为 table 中的列,并且属性的值成为行数据。

我是 XML 的新手,所以任何提示都是有用的!非常感谢!

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

根据要求。