如何使用xslt转换xml文档以根据子节点复制输出行

时间:2017-07-04 14:23:42

标签: xml xslt ssis

我致力于使用SSIS包提取Icecat xml文件的信息。

<?xml version="1.0" encoding="UTF-8"?>
<ICECAT-interface>
<Response Date="Tue Jun 27 16:00:01 2017" ID="0" Request_ID="1498572001" 
Status="1">
<SuppliersList>
  <Supplier ID="1" LogoPic="http://images.icecat.biz/img/brand/thumb/1_91e0e5b25690437cb026012c624a00ad.jpg" LogoPicHeight="75" LogoPicWidth="75" LogoPicSize="10201" LogoLowPic="http://images.icecat.biz/img/brand/low/1_577ba75b274e4917bf27f4ff5fc609be.jpg" LogoLowPicHeight="200" LogoLowPicWidth="200" LogoLowSize="31058" LogoMediumPic="" LogoMediumPicHeight="0" LogoMediumPicWidth="0" LogoMediumPicSize="0" LogoHighPic="http://images.icecat.biz/img/brand/high/1_43ebe562b309447dbbf377fd5adf41e3.jpg" LogoHighPicHeight="200" LogoHighPicWidth="200" LogoHighPicSize="31058" LogoOriginal="http://images.icecat.biz/img/brand/original/1_9a451c84f85c44d78ae79a1712706fe9.jpg" LogoOriginalSize="31133" Name="HP" Sponsor="1">
    <Names>
      <Name langid="12" Name="惠普" />
    </Names>
  </Supplier>
  <Supplier ID="2" LogoPic="http://images.icecat.biz/img/brand/thumb/2_b4222cd1f4ef41a79d452c7d47d9b8a8.jpg" LogoPicHeight="75" LogoPicWidth="75" LogoPicSize="3668" LogoLowPic="http://images.icecat.biz/img/brand/low/2_7a45daa4e872400c8037f0e4231a7cb4.jpg" LogoLowPicHeight="53" LogoLowPicWidth="200" LogoLowSize="13241" LogoMediumPic="" LogoMediumPicHeight="0" LogoMediumPicWidth="0" LogoMediumPicSize="0" LogoHighPic="http://images.icecat.biz/img/brand/high/2_286ef5dd29ca4941be0b9de27f041b4b.jpg" LogoHighPicHeight="53" LogoHighPicWidth="200" LogoHighPicSize="13241" LogoOriginal="http://images.icecat.biz/img/brand/original/2_093830b1b15a4eb8b38425d64ba80e9d.jpg" LogoOriginalSize="37929" Name="Toshiba" Sponsor="1" />
  <Supplier ID="3" LogoPic="http://images.icecat.biz/img/brand/thumb/3_d2375bf6ef1d47269cd01fa902a13788.jpg" LogoPicHeight="75" LogoPicWidth="75" LogoPicSize="3382" LogoLowPic="http://images.icecat.biz/img/brand/low/3_d05e0e4693d34b85ad56c94404338662.jpg" LogoLowPicHeight="76" LogoLowPicWidth="200" LogoLowSize="9156" LogoMediumPic="" LogoMediumPicHeight="0" LogoMediumPicWidth="0" LogoMediumPicSize="0" LogoHighPic="http://images.icecat.biz/img/brand/high/3_a4caad745868496ea1ee4e6c0df57585.jpg" LogoHighPicHeight="76" LogoHighPicWidth="200" LogoHighPicSize="9156" LogoOriginal="http://images.icecat.biz/img/brand/original/3_b5bc53009d974136ba7048b8e2d9e8b4.jpg" LogoOriginalSize="37257" Name="IBM" Sponsor="1" />
  <Supplier ID="5" LogoPic="http://images.icecat.biz/img/brand/thumb/5_1cf5d422c33b47159d29120556249309.jpg" LogoPicHeight="75" LogoPicWidth="75" LogoPicSize="1916" LogoLowPic="http://images.icecat.biz/img/brand/low/5_a028484b02804360a3b2a3d6f1635439.jpg" LogoLowPicHeight="43" LogoLowPicWidth="200" LogoLowSize="6322" LogoMediumPic="" LogoMediumPicHeight="0" LogoMediumPicWidth="0" LogoMediumPicSize="0" LogoHighPic="http://images.icecat.biz/img/brand/high/5_5666d3e455e34cdea7572e6ab5a4e38e.jpg" LogoHighPicHeight="43" LogoHighPicWidth="200" LogoHighPicSize="6322" LogoOriginal="http://images.icecat.biz/img/brand/original/5_b1c151c0bacf4360b4eb9161b71ea8a4.jpg" LogoOriginalSize="6510" Name="Sony" Sponsor="1" />
  <Supplier ID="6" LogoPic="http://images.icecat.biz/img/brand/thumb/6_bed73021a8fc496a8386b3c9276f0216.jpg" LogoPicHeight="75" LogoPicWidth="75" LogoPicSize="3111" LogoLowPic="http://images.icecat.biz/img/brand/low/6_8dae0e5b024145aa82c5c229048240e8.jpg" LogoLowPicHeight="117" LogoLowPicWidth="200" LogoLowSize="11196" LogoMediumPic="" LogoMediumPicHeight="0" LogoMediumPicWidth="0" LogoMediumPicSize="0" LogoHighPic="http://images.icecat.biz/img/brand/high/6_53e052f2b7594a4e9f3950081dfe143d.jpg" LogoHighPicHeight="117" LogoHighPicWidth="200" LogoHighPicSize="11196" LogoOriginal="http://images.icecat.biz/img/brand/original/6_f4b347ab7d2e4ab88f84d8e04ccfe966.jpg" LogoOriginalSize="24384" Name="3com" />
  <Supplier ID="7" LogoPic="http://images.icecat.biz/img/brand/thumb/7_2e1091176d2d450eae2dce7506f1952f.jpg" LogoPicHeight="75" LogoPicWidth="75" LogoPicSize="3583" LogoLowPic="http://images.icecat.biz/img/brand/low/7_5760a89b29ab45aeae95ae639c247b3a.jpg" LogoLowPicHeight="89" LogoLowPicWidth="200" LogoLowSize="11942" LogoMediumPic="http://images.icecat.biz/img/brand/medium/7_ce2e94986d1847568b47a2d488d374fd.jpg" LogoMediumPicHeight="222" LogoMediumPicWidth="500" LogoMediumPicSize="31489" LogoHighPic="http://images.icecat.biz/img/brand/high/7_faa735acc8b7487093b094c1440cc91d.jpg" LogoHighPicHeight="1336" LogoHighPicWidth="2999" LogoHighPicSize="241523" LogoOriginal="http://images.icecat.biz/img/brand/original/7_6a89e510401c47d894ad4bb6188cc65a.jpg" LogoOriginalSize="442120" Name="Acer" Sponsor="1">
    <Names>
      <Name langid="12" Name="宏碁" />
    </Names>
  </Supplier>
  <Supplier ID="8" LogoPic="http://images.icecat.biz/img/brand/thumb/8_d489e504070b40e886b6456a0a7beb99.jpg" LogoPicHeight="75" LogoPicWidth="75" LogoPicSize="6122" LogoLowPic="http://images.icecat.biz/img/brand/low/8_d5179e6b3bb14bf889d5bf56e2ed42e3.jpg" LogoLowPicHeight="200" LogoLowPicWidth="158" LogoLowSize="20437" LogoMediumPic="" LogoMediumPicHeight="0" LogoMediumPicWidth="0" LogoMediumPicSize="0" LogoHighPic="http://images.icecat.biz/img/brand/high/8_e7673fd830a04b2fa8f655bb58dad630.jpg" LogoHighPicHeight="252" LogoHighPicWidth="200" LogoHighPicSize="25045" LogoOriginal="http://images.icecat.biz/img/brand/original/8_a711999f9d3a4c8fb35fec28f22d4dbf.jpg" LogoOriginalSize="46934" Name="Adobe" />
  <Supplier ID="9" LogoPic="" LogoPicHeight="0" LogoPicWidth="0" LogoPicSize="0" LogoLowPic="" LogoLowPicHeight="0" LogoLowPicWidth="0" LogoLowSize="0" LogoMediumPic="" LogoMediumPicHeight="0" LogoMediumPicWidth="0" LogoMediumPicSize="0" LogoHighPic="" LogoHighPicHeight="0" LogoHighPicWidth="0" LogoHighPicSize="0" LogoOriginal="" LogoOriginalSize="0" Name="Apple">
    <Names>
      <Name langid="12" Name="苹果" />
    </Names>
  </Supplier>
 ....

就像你有时候我看到供应商的名字就像attibutes(它是供应商的国际名称langid = 1 =英语)但有时候我是孩子节点&#34;名字&# 34;用中文这样的特殊语言来命名...

我希望在我的子节点喜欢这个时创建一个新的xml文件,其中包含Name和LangId attibutes重复行:

  <Supplier ID="1" LogoLowPic="http://images.icecat.biz/img/brand/low/1_577ba75b274e4917bf27f4ff5fc609be.jpg" Name="HP" langid="1"></Supplier>
    <Supplier ID="1" LogoLowPic="http://images.icecat.biz/img/brand/low/1_577ba75b274e4917bf27f4ff5fc609be.jpg" Name="惠普" langid="12"></Supplier>
    <Supplier ID="2"  LogoLowPic="http://images.icecat.biz/img/brand/low/2_7a45daa4e872400c8037f0e4231a7cb4.jpg"  Name="Toshiba" langid="1"></Supplier>

我试试,但这不是好结果。

<?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="SuppliersList"/>
</xsl:template>

<xsl:template match="SuppliersList">
    <xsl:apply-templates select="Supplier"/>
</xsl:template>

<xsl:template match="Supplier">
    <xsl:element name="Supplier">
        <xsl:attribute name="ID">
            <xsl:value-of select="@ID"/>
        </xsl:attribute>
        <xsl:attribute name="LogoLowPic">
            <xsl:value-of select="@LogoLowPic"/>
        </xsl:attribute>
        <xsl:attribute name="Name">
            <xsl:value-of select="@Name"/>
        </xsl:attribute>
        <xsl:attribute name="langid">
            <xsl:value-of select="1"/>
        </xsl:attribute>
        <xsl:apply-templates select="Names"/>
    </xsl:element>
</xsl:template>
 <xsl:template match="Names">
    <xsl:element name="Name">
        <xsl:attribute name="Name">
            <xsl:value-of select="@Name"/>
        </xsl:attribute>
        <xsl:attribute name="langid">
            <xsl:value-of select="@langid"/>
        </xsl:attribute>
    </xsl:element>
 </xsl:template>
</xsl:stylesheet>

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您需要做的一件事是将<xsl:apply-templates select="Names"/>移到复制Supplier元素之后,而不是复制在其中,这样您就可以创建一个兄弟,而不是一个孩子。

如果您选择Names/Name而不仅仅是Name,也会更容易。

试试这个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" />
    <xsl:copy-of select="@Name|@langid" />
  </Supplier>
 </xsl:template>
</xsl:stylesheet>

请注意我是如何使用xsl:copy-of来简化属性复制的。或者,您可以在此处使用属性值模板。例如,Name模板可以这样写。

<xsl:template match="Name">
  <Supplier ID="{../../@ID}">
    <xsl:copy-of select="@Name|@langid" />
  </Supplier>
 </xsl:template>