使用xslt

时间:2016-04-21 16:30:32

标签: xml xslt ssis

如何使用样式表将XML元素标记名称替换为属性值? XML文件结构相当简单,我想使用元素中的属性值替换元素标记名称(DataColumn)。我正在与SSIS合作。 XML文件是数据源。我理解可能有多种方法来解析XML文件,但我正在寻找SSIS中的XML转换方法。我能够提出的替代解决方案是PIVOT组件。但是,如果XML文件包含30个或更多元素,则变得非常简单。这是XML结构。我有缩写内容,所以它不会太冗长。

<?xml version="1.0" encoding="utf-8"?>
<RootNode>
  <ResponseHeader />
  <CustomReport Name="Sample Test" ID="000">
    <DataRow>
      <DataColumn Name="Order ID">12345654</DataColumn>
      <DataColumn Name="Company ID">656584</DataColumn>
      <DataColumn Name="First Name">John</DataColumn>
      <DataColumn Name="Last Name">Smith</DataColumn>
      <DataColumn Name="Email">john.smith@domain.com</DataColumn>
      <DataColumn Name="Address 1">13542 S Main Street</DataColumn>
      <DataColumn Name="Address 2"></DataColumn>
      <DataColumn Name="City">Dallas</DataColumn>
      <DataColumn Name="State">TX</DataColumn>
      <DataColumn Name="Zip">75236</DataColumn>
      <DataColumn Name="Country">United States</DataColumn>
      <DataColumn Name="Phone">000-000-0000</DataColumn>
      <DataColumn Name="Fax"></DataColumn>
    </DataRow>
    <DataRow>
      <DataColumn Name="Order ID">5432354</DataColumn>
      <DataColumn Name="Company ID">545454</DataColumn>
      <DataColumn Name="First Name">Jane</DataColumn>
      <DataColumn Name="Last Name">Smith</DataColumn>
      <DataColumn Name="Email">jane.smith@domain.com</DataColumn>
      <DataColumn Name="Address 1">213213 W Main Blvd.</DataColumn>
      <DataColumn Name="Address 2"></DataColumn>
      <DataColumn Name="City">Baltimore</DataColumn>
      <DataColumn Name="State">MD</DataColumn>
      <DataColumn Name="Zip">21207</DataColumn>
      <DataColumn Name="Country">United States</DataColumn>
      <DataColumn Name="Phone">000-000-0000</DataColumn>
      <DataColumn Name="Fax"></DataColumn>
    </DataRow>
  </CustomReport>
</RootNode>

这是所需的输出:

<?xml version="1.0" encoding="utf-8"?>
    <RootNode>
      <ResponseHeader />
      <CustomReport Name="Sample Test" ID="000">
        <DataRow>
          <Order ID>12345654</Order ID>
          <Company ID>656584</Company ID>
          <First Name>John</First Name>
          <Last Name>Smith</Last NAme>
          <Email>john.smith@domain.com</Email>
          <Address 1>13542 S Main Street</Address 1>
          <Address 2></Address2>
          <City>Dallas</City>
          <State>TX</State>
          <Zip>75236</Zip>
          <Country>United States</Country>
          <Phone>000-000-0000</Phone>
          <Fax></Fax>
        </DataRow>
        <DataRow>
          <Order ID>5432354</Order ID>
          <Company ID>545454</Company ID>
          <First Name>Jane</First Name>
          <Last Name>Smith</Last Name>
          <Email>jane.smith@domain.com</Email>
          <Address 1>213213 W Main Blvd.</Address 1>
          <Address 2></Address 2>
          <City>Baltimore</City>
          <State>MD</State>
          <Zip>21207</Zip>
          <Country>United States</Country>
          <Phone>000-000-0000</Phone>
          <Fax></Fax>
        </DataRow>
      </CustomReport>
    </RootNode>

如果此主题已经发布并在之前得到解答,请指向正确的链接。谢谢!

2 个答案:

答案 0 :(得分:2)

根据我上面的评论,您可以尝试:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="DataColumn">
    <xsl:element name="{translate(@Name, ' ', '_')}">
        <xsl:value-of select="." />
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

应用于给定的输入示例,结果为:

<?xml version="1.0" encoding="UTF-8"?>
<RootNode>
   <ResponseHeader/>
   <CustomReport Name="Sample Test" ID="000">
      <DataRow>
         <Order_ID>12345654</Order_ID>
         <Company_ID>656584</Company_ID>
         <First_Name>John</First_Name>
         <Last_Name>Smith</Last_Name>
         <Email>john.smith@domain.com</Email>
         <Address_1>13542 S Main Street</Address_1>
         <Address_2/>
         <City>Dallas</City>
         <State>TX</State>
         <Zip>75236</Zip>
         <Country>United States</Country>
         <Phone>000-000-0000</Phone>
         <Fax/>
      </DataRow>
      <DataRow>
         <Order_ID>5432354</Order_ID>
         <Company_ID>545454</Company_ID>
         <First_Name>Jane</First_Name>
         <Last_Name>Smith</Last_Name>
         <Email>jane.smith@domain.com</Email>
         <Address_1>213213 W Main Blvd.</Address_1>
         <Address_2/>
         <City>Baltimore</City>
         <State>MD</State>
         <Zip>21207</Zip>
         <Country>United States</Country>
         <Phone>000-000-0000</Phone>
         <Fax/>
      </DataRow>
   </CustomReport>
</RootNode>

但是,如果名称以数字开头,这也会失败,例如。

答案 1 :(得分:0)

您无法做到这一点,因为某些Names属性具有空格,并且不允许这些属性作为元素名称。 你不能拥有元素<Last Name>Smith</Last Name> 但是,你可以用另一个字符替换空格,如 - 这应该这样做:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  >

  <xsl:template match="RootNode|CustomReport|DataRow">
   <xsl:element name="{name()}">
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates/>
   </xsl:element>
  </xsl:template>

  <xsl:template match="DataColumn">
   <xsl:variable name='nn' select="translate(@Name,' ','-')"/>
   <xsl:element name="{$nn}">
    <xsl:apply-templates select="*"/>
    <xsl:apply-templates/>
   </xsl:element>
  </xsl:template>

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

  <xsl:template match="text()">
    <xsl:value-of select='.'/>
  </xsl:template>


</xsl:stylesheet>

给出了:

<?xml version="1.0"?>
<RootNode>

  <CustomReport Name="Sample Test" ID="000">
    <DataRow>
      <Order-ID>12345654</Order-ID>
      <Company-ID>656584</Company-ID>
      <First-Name>John</First-Name>
      <Last-Name>Smith</Last-Name>
      <Email>john.smith@domain.com</Email>
      <Address-1>13542 S Main Street</Address-1>
      <Address-2/>
      <City>Dallas</City>
      <State>TX</State>
      <Zip>75236</Zip>
      <Country>United States</Country>
      <Phone>000-000-0000</Phone>
      <Fax/>
    </DataRow>
    <DataRow>
      <Order-ID>5432354</Order-ID>
      <Company-ID>545454</Company-ID>
      <First-Name>Jane</First-Name>
      <Last-Name>Smith</Last-Name>
      <Email>jane.smith@domain.com</Email>
      <Address-1>213213 W Main Blvd.</Address-1>
      <Address-2/>
      <City>Baltimore</City>
      <State>MD</State>
      <Zip>21207</Zip>
      <Country>United States</Country>
      <Phone>000-000-0000</Phone>
      <Fax/>
    </DataRow>
  </CustomReport>
</RootNode>