2017-07-25 68 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