Filter dynamically xml child element with xslt wit

2019-07-29 10:44发布

问题:

I've a big xml file from ICECAT. And I want take only some informations. It's in the following of this subject how transform xml document with xslt to duplicate output lines according to the child node

I've in Database a table of language. And I want with xslt filter child elements <Name> depending of my table content.

I'm in SSIS project.

回答1:

1/I create a variable named Filter and a Foreach ADO Enumerator putting enuration in variable IdLang

2/ Use a expression task with this expression : @[User::Filter]=( LEN( @[User::Filter] ) ==0 ? "@langid=" + (DT_WSTR, 2) @[User::IdLang] : @[User::Filter] + " or @langid=" + (DT_WSTR, 2)@[User::IdLang] )

3/ In old subject, I've an xslt file, what I put in a variable named "xslt":

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output  method="xml" encoding="UTF-8" indent="yes"/>  <xsl:template match="/ICECAT-interface">     <xsl:apply-templates select="Response"/> </xsl:template>  <xsl:template match="Response">     <xsl:apply-templates select="SuppliersList"/> </xsl:template>  <xsl:template match="SuppliersList">   <xsl:copy>     <xsl:apply-templates select="Supplier"/>   </xsl:copy> </xsl:template>  <xsl:template match="Supplier">     <Supplier>       <xsl:copy-of select="@ID|@LogoLowPic|@Name"/>       <xsl:attribute name="langid">           <xsl:value-of select="1"/>       </xsl:attribute>   </Supplier>   <xsl:apply-templates select="Names/Name"/> </xsl:template>  <xsl:template match="Name[###]">   <Supplier>     <xsl:copy-of select="../../@ID|../../@LogoLowPic|@langid|@Name" /> </Supplier>  </xsl:template> </xsl:stylesheet>

4/ ANd finally I use script task

Dim filtr As String = Dts.Variables("User::Filter").Value 

        Dim Schem = Dts.Variables("User::Xslt").Value.ToString.Replace("###", filtr)
        Dim xslt As New XslCompiledTransform()
        xslt.Load(New XmlTextReader(New IO.StringReader(Schem)))
         Dim settings As New Xml.XmlReaderSettings()
        settings.DtdProcessing = Xml.DtdProcessing.Parse
        Dim SourcePath As String = ???
        Dim source As Xml.XmlReader = Xml.XmlReader.Create(SourcePath, settings)

        Dim DestinationPath As String = ???
        Dim Destination As Xml.XmlWriter = Xml.XmlWriter.Create(DestinationPath)

        xslt.Transform(source, Destination)

I Hope It can helping someone.