2017-02-24 99 views
1

我是xslt的新手,我正在从事xml到xml的转换工作。请为下面的问题提供一个xslt解决方案。 输入XML是如下:使用xslt将XML转换为XML,使用属性名称替换元素名称

<root type="object"> 
    <items type="array"> 
     <item type="object"> 
      <original_file_name type="string">filename-m.mp3</original_file_name> 
      <description type="string">some description text</description> 
      <created_at type="string">2017-02-20T20:52:52Z</created_at> 
      <metadata type="object"> 
       <guest type="string">guestname here</guest> 
       <webInfo type="string">http://abc</webInfo> 
       <title type="string">title text testing</title> 
       <airDate type="string">2017-02-21</airDate> 
      </metadata> 
      <status type="string">live</status> 
      <asset_type type="string">video</asset_type> 
      <player_id type="string">391e099a718f4a62b44c78f97f85ecde</player_id> 
      <name type="string">title</name> 
     </item> 
     <item type="object"> 
      <original_file_name type="string">filename-m.mp3111</original_file_name> 
      <description type="string">some description text test</description> 
      <created_at type="string">2015-02-20T20:52:52Z</created_at> 
      <metadata type="object"> 
       <guest type="string">guestname here 1111</guest> 
       <webInfo type="string">http://abc</webInfo> 
       <a:item type="string" item="album description" xmlns:a="item">test description album</a:item> 
       <a:item type="string" item="album order" xmlns:a="item">106</a:item> 
      </metadata> 
      <status type="string">live</status> 
      <asset_type type="string">video</asset_type> 
      <player_id type="string">391e099a718f4a62b44c78f97f85ecdea</player_id> 
      <name type="string">title1</name>   
     </item> 
    </items> 
</root> 

输出XML需要是如下:

<assets> 
    <item> 
     <original_file_name>filename-m.mp3</original_file_name> 
     <description>some description text</description> 
     <created_at>2017-02-20T20:52:52Z</created_at> 
     <guest>guestname here</guest> 
     <webInfo>http://abc</webInfo> 
     <title>title text testing</title> 
     <airDate>2017-02-21</airDate> 
     <status type="string">live</status> 
     <asset_type type="string">video</asset_type> 
     <player_id type="string">391e099a718f4a62b44c78f97f85ecde</player_id> 
     <name type="string">title</name> 
    </item> 
    <item> 
     <original_file_name>filename-m.mp3111</original_file_name> 
     <description>some description text test</description> 
     <created_at>2015-02-20T20:52:52Z</created_at> 
     <guest>guestname here 1111</guest> 
     <webInfo>http://abc</webInfo> 
     <album_description>test description album</album_description> 
     <album_order>106</album_order> 
     <status type="string">live</status> 
     <asset_type type="string">video</asset_type> 
     <player_id type="string">391e099a718f4a62b44c78f97f85ecdea</player_id> 
     <name type="string">title1</name> 
    </item> 
</assets> 

子元数据的节点被动态元素,元素名称和编号将在每个子节点不同的元数据。 下面是输出文件的更改:

  1. 替换资产根/项目
  2. 删除元数据的元数据标签和子节点成为项目的直接孩子
  3. 当元数据的子节点有: item元素然后用属性item =“album description”替换元素名称 属性值有空格,所以我们需要用下划线替换空格。

我需要有点数的解决方案3.

谢谢

回答

0
<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:a="item"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<!-- replace root/items with assets --> 
<xsl:template match="root/items"> 
    <xsl:element name="assets"> 
    <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 

<!-- for some elements, copy only inner nodes, not attributes --> 
<xsl:template match="item|original_file_name|description|created_at|guest|webInfo|title|airDate"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()"/> 
    </xsl:copy> 
</xsl:template> 

<!-- for metadata tag replace it with its children --> 
<xsl:template match="metadata">  
    <xsl:apply-templates select="node()"/> 
</xsl:template> 

<!-- for a:item tag emit new element with name taken from item attribute --> 
<xsl:template match="a:item">  
    <xsl:element name="{translate(@item, ' ', '_')}"> 
    <xsl:apply-templates select="node()"/> 
    </xsl:element> 
</xsl:template> 

<!--Identity template, provides default behavior that copies all content into the output --> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
+0

我得到一个错误消息”‘:’字符,十六进制值0x3A,不能包含在名称“在行

+0

它取决于您正在使用的xslt处理器。我在xalan和撒克逊人测试了它,它对两者都有效。 – Lesiak

+0

我在SSIS(Microsoft visual studio 2010)中使用XML任务。我不知道如何验证xslt处理器 –