Awadhendra
Awadhendra

Reputation: 355

Transform xml data using xslt file

I want to remove metadata and schema information from a given xml file using xslt so that I can import those data in sql table but I was not able to remove it's metadata and schema info from xml file. I did lot of googles actually from last three days but I am not able to perform it. Please provide me a valid xslt file code which remove meta data and schema from given xml file.

Here I am providing you code of xml file.

<?xml version="1.0" encoding="utf-16"?>
<DataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="lukas_id_nagrody" type="xs:string" minOccurs="0" />
                <xs:element name="ilosc" type="xs:int" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    <NewDataSet>
      <Table diffgr:id="Table1" msdata:rowOrder="0">
        <lukas_id_nagrody>10</lukas_id_nagrody>
        <ilosc>4</ilosc>
      </Table>
      <Table diffgr:id="Table2" msdata:rowOrder="1">
        <lukas_id_nagrody>12</lukas_id_nagrody>
        <ilosc>10</ilosc>
      </Table>
      <Table diffgr:id="Table3" msdata:rowOrder="2">
        <lukas_id_nagrody>21</lukas_id_nagrody>
        <ilosc>32</ilosc>
      </Table>
      <Table diffgr:id="Table4" msdata:rowOrder="3">
        <lukas_id_nagrody>32</lukas_id_nagrody>
        <ilosc>13</ilosc>
      </Table>
      <Table diffgr:id="Table5" msdata:rowOrder="4">
        <lukas_id_nagrody>33</lukas_id_nagrody>
        <ilosc>15</ilosc>
      </Table>
      <Table diffgr:id="Table6" msdata:rowOrder="5">
        <lukas_id_nagrody>34</lukas_id_nagrody>
        <ilosc>2</ilosc>
      </Table>
    </NewDataSet>
  </diffgr:diffgram>
</DataSet>

I am using xml task in SSIS.

After transforming above xml data I want following output.

<NewDataSet>
    <Table>
            <lukas_id_nagrody>34</lukas_id_nagrody>
            <ilosc>2</ilosc>
    </Table>
</NewDataSet>

I want output something like this. Thanks.

Upvotes: 0

Views: 1159

Answers (1)

Mitya
Mitya

Reputation: 34586

You need a simplified identity/copy pattern, starting from NewDataSet and omitting attributes. Try this:

<xsl:template match="DataSet/diffgr:diffgram/NewDataSet|DataSet/diffgr:diffgram/NewDataSet//*|text()">
    <xsl:copy>
        <xsl:apply-templates select='*|text()' />
    </xsl:copy>
</xsl:template>

Runnable demo (see output source)

Upvotes: 1

Related Questions