Build XML file with XSLT to filter on severals childs nodes -
i've big xml file icecat. , want take informations. it's in following of subject filter-dynamically-xml-child-element-with-xslt-with-ssis
now i've categorieslist xml file this:
<icecat-interface> <response date="tue jul 25 16:00:10 2017" id="29306604" request_id="1500991209" status="1"> <categorieslist> <category id="2597" lowpic="http://images.icecat.biz/img/low_pic/2597-5095.jpg" score="471102" searchable="0" thumbpic="http://images.icecat.biz/thumbs/cat2597.jpg" uncatid="43223323" visible="0"> <name id="1088701" value="fiber optic adapters" langid="1"/> <name id="1595015" value="glasvezeladapters" langid="2"/> <name id="1088703" value="adaptateurs de fibres optiques" langid="3"/> <name id="1245208" value="lwl-steckverbinder" langid="4"/> <name id="1088705" value="adattatori di fibra ottica" langid="5"/> <name id="1125574" value="adaptadores de fibra óptica" langid="6"/> <name id="1147616" value="lyslederadapter" langid="7"/> <parentcategory id="242"> <names> <name id="485" langid="1">networking</name> <name id="471244" langid="2">netwerken</name> <name id="343986" langid="3">réseaux</name> <name id="436999" langid="4">netzwerke</name> <name id="1051724" langid="5">reti</name> <name id="1041258" langid="6">redes</name> <name id="34261" langid="7">netværk</name> <name id="530435" langid="8">сети/коммуникации</name> </names> </parentcategory> </category> <category id="4601" lowpic="http://images.icecat.biz/img/low_pic/4601-990.jpg" score="12621" searchable="0" thumbpic="http://images.icecat.biz/thumbs/cat4601.jpg" uncatid="56101688" visible="0">
i need attributes in category
node id
, lowpic
... name
nodes , id
parentcategory
node.
i tried 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" 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="categorieslist"/> </xsl:template> <xsl:template match="categorieslist"> <xsl:copy> <xsl:apply-templates select="category"/> </xsl:copy> </xsl:template> <xsl:template match="category"> <xsl:apply-templates select="name"/> </xsl:template> <xsl:template match="name[@langid=1 or @langid=3]"> <category> <xsl:copy-of select="../@id|../@lowpic|../@thumbpic|../@uncatid||@langid|@value" /> </category> </xsl:template> </xsl:stylesheet>
i don't know if it's better methods , don't have id
of parentcategory
node.
update sorry forgot result want
<?xml version="1.0" encoding="utf-8"?> <categories> <category id="2597" lowpic="http://images.icecat.biz/img/low_pic/2597-5095.jpg" thumbpic="http://images.icecat.biz/thumbs/cat2597.jpg" uncatid="43223323" name="fiber optic adapters" langid="1" parentcategory="242"/> <category id="2597" lowpic="http://images.icecat.biz/img/low_pic/2597-5095.jpg" thumbpic="http://images.icecat.biz/thumbs/cat2597.jpg" uncatid="43223323" name="adaptateurs de fibres optiques" langid="3" parentcategory="242"/> ....
update 2 modify xslt file inverse filter position. i've goods records, lake of parentcategory id
it looks want output category
each name
langid
attribute of 1 or 3. in case need move condition xsl:apply-templates
<xsl:template match="category"> <xsl:apply-templates select="name[@langid=1 or @langid=3]"/> </xsl:template>
then, within template matching name
can create attribute parentcategoryid so
<xsl:attribute name="parentcategoryid"> <xsl:value-of select="following-sibling::parentcategory[1]/@id" /> </xsl:attribute>
and name
attribute.
try 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="categorieslist"/> </xsl:template> <xsl:template match="categorieslist"> <xsl:copy> <xsl:apply-templates select="category"/> </xsl:copy> </xsl:template> <xsl:template match="category"> <xsl:apply-templates select="name[@langid=1 or @langid=3]"/> </xsl:template> <xsl:template match="name"> <category> <xsl:copy-of select="../@id|../@lowpic|../@thumbpic|../@uncatid|@langid" /> <xsl:attribute name="name"> <xsl:value-of select="@value" /> </xsl:attribute> <xsl:attribute name="parentcategoryid"> <xsl:value-of select="following-sibling::parentcategory[1]/@id" /> </xsl:attribute> </category> </xsl:template> </xsl:stylesheet>
note, can simplify name
template using attribute value templates
<xsl:template match="name"> <category name="{@value}" parentcategoryid="{following-sibling::parentcategory[1]/@id}"> <xsl:copy-of select="../@id|../@lowpic|../@thumbpic|../@uncatid|@langid" /> </category> </xsl:template>
Comments
Post a Comment