2017-07-25 67 views
0

我有一个来自ICECAT的大XML文件。我只想要一些信息。它在这个问题filter-dynamically-xml-child-element-with-xslt-with-ssis使用XSLT构建XML文件以筛选多个子节点

现在我已经像这样的categoriesList XML文件的以下:

<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"> 

我需要一些属性,像IDLowPic ......一些Name节点和IDCategory节点ParentCategory节点。

我尝试这样做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> 

我不知道这是否是更好的方法,我没有ParentCategory节点的ID

UPDATE 对不起,我忘了,结果我想要的东西

<?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"/> 
.... 

更新2 我修改XSLT文件我反我的过滤器位置。现在,我的货物记录,parentcategory ID的只是湖

+0

如何将所需输出样子简化Name模板? – zx485

+0

您能确保您的输出与您的输入完全一致吗?您当前的预期输出引用不在您输入内容的文字“其他手机”。如果你的输入XML格式良好,它也会有所帮助。目前它缺少一些结束标签。谢谢! –

+0

我这样做。我改变第一篇文章中的例子 – YannickIngenierie

回答

1

它看起来像你想输出Category每个Name为1或3. langid属性在这种情况下,你需要的条件移至xsl:apply-templates

<xsl:template match="Category"> 
    <xsl:apply-templates select="Name[@langid=1 or @langid=3]"/> 
</xsl:template> 

然后,匹配Name在模板中,你可以像这样

<xsl:attribute name="ParentCategoryId"> 
    <xsl:value-of select="following-sibling::ParentCategory[1]/@ID" /> 
</xsl:attribute> 

为创建为ParentCategoryId一个属性,同样属性。

试试这个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> 

注意,您可以通过使用属性值模板

<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> 
+0

超级,但我不知道如果父类别会一直在之后,那么跟随兄弟可能是不够的。然后我替换为'' – YannickIngenierie

+0

我在空值上添加filtrer:'' – YannickIngenierie