使用 xslt 和 SSIS 动态过滤 xml 个子元素
Filter dynamically xml child element with xslt with SSIS
我有一个来自 ICECAT 的大 xml 文件。我只想获取一些信息。它在这个主题的下面
我在数据库中有一种 table 语言。我想要根据我的 table 内容使用 xslt 过滤器子元素 <Name>
。
我在 SSIS 项目中。
1/我创建一个名为 Filter 的变量和一个 Foreach ADO 枚举器,将枚举放入变量 IdLang
2/ 使用带有此表达式的表达式任务:
@[User::Filter]=( LEN( @[User::Filter] ) ==0 ? "@langid=" + (DT_WSTR, 2) @[User::IdLang] : @[User::Filter] + " or @langid=" + (DT_WSTR, 2)@[User::IdLang] )
3/ 在旧主题中,我有一个 xslt 文件,我将其放入名为 "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/ 最后我使用脚本任务
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)
我希望它可以帮助别人。
我有一个来自 ICECAT 的大 xml 文件。我只想获取一些信息。它在这个主题的下面
我在数据库中有一种 table 语言。我想要根据我的 table 内容使用 xslt 过滤器子元素 <Name>
。
我在 SSIS 项目中。
1/我创建一个名为 Filter 的变量和一个 Foreach ADO 枚举器,将枚举放入变量 IdLang
2/ 使用带有此表达式的表达式任务:
@[User::Filter]=( LEN( @[User::Filter] ) ==0 ? "@langid=" + (DT_WSTR, 2) @[User::IdLang] : @[User::Filter] + " or @langid=" + (DT_WSTR, 2)@[User::IdLang] )
3/ 在旧主题中,我有一个 xslt 文件,我将其放入名为 "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/ 最后我使用脚本任务
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)
我希望它可以帮助别人。