如何将原子进给中的所需字段转换为xml?

时间:2014-12-10 11:52:34

标签: xml xslt atom-feed

我有一个包含大量信息的原子提要,我想将所需的信息转换为XML。 java代码已经完成,但我无法为此转换创建样式表。

<feed version="1.0" xmlns="http://www.w3.org/2005/Atom" xmlns:wwe="http://release.wwe.com/atom/1.0">
      <id>http://release.wwe.com/wweatomfeed.atom</id>
      <title><![CDATA[WWE Syndication Feed]]></title>
      <subtitle>WWE Feed to syndicate WWE assets to domestic as well as international partners. This feed has custom modules for accommodating custom data transfer to clients.</subtitle>
      <author><![CDATA[Roxy.Noordeen@wwecorp.com]]></author>
      <icon>http://release.wwe.com/media/storage_images/wwe-icon.jpg</icon>
      <logo>http://release.wwe.com/media/storage_images/wwe-logo.jpg</logo>
      <rights>2011 World Wrestling Entertainment!</rights>
      <entry>
        <id>26168987_26169027</id>
        <title><![CDATA[WrestleMania 31]]></title>
        <updated>2015-03-29T19:00:00-0400</updated>
        <category term="WrestleMania"/>
        <wwe:asset_type>Article</wwe:asset_type>
        <author><![CDATA[john clapp]]></author>
        <published>2015-03-29T19:00:00-0400</published>
        <content><![CDATA[
    Complete WrestleMania 31 coverage, news, videos, photos and more.
    ]]></content>
        <wwe:shortSummary/>
        <wwe:body><![CDATA[

    ]]></wwe:body>
        <wwe:breadCrumb>Homepage#Shows#Wrestlemania#31</wwe:breadCrumb>
      </entry>
      <entry>
        <id>26663863_26663903</id>
        <title><![CDATA[WWE TLC 2014]]></title>
        <updated>2014-12-14T20:00:00-0500</updated>
        <category term="WWE TLC: Tables, Ladders and Chairs ... and Stairs"/>
        <wwe:asset_type>Article</wwe:asset_type>
        <author><![CDATA[john clapp]]></author>
        <published>2014-12-14T20:00:00-0500</published>
        <content><![CDATA[
    WWE TLC: Tables, Ladders, and Chairs … and Stairs 2014 takes over Cleveland's Quicken Loans Arena on Sunday, Dec. 14, at 8 p.m. ET/5 p.m. PT.
    ]]></content>
        <wwe:shortSummary/>
        <wwe:body><![CDATA[

    ]]></wwe:body>
        <wwe:breadCrumb>Homepage#Shows#Wwetlc#2014</wwe:breadCrumb>
      </entry>
      <entry>
        <id>26898092_26898132</id>
        <title><![CDATA[The Most Shocking Tables, Ladders & Chairs Moments]]></title>
        <updated>2014-12-09T16:33:00-0500</updated>
        <category term="Non-Branded"/>
        <wwe:asset_type>Video</wwe:asset_type>
        <link rel="enclosure" type="image/jpeg" href="http://us.wwe.com/f/video/thumb/2014/12/cdown_tlc_moments_updatedlogo.jpg"/>
        <link rel="enclosure" type="video/x-flv" href="http://release.wwe.com/media/storage_video/26898092.flv"/>
        <link rel="enclosure" type="video/mp4" href="http://release.wwe.com/media/storage_video/26898092.f4v"/>
        <link rel="enclosure" type="video/x-ms-wmv" href="http://release.wwe.com/media/storage_video/26898092.wmv"/>
        <link rel="enclosure" type="video/h264" href="http://release.wwe.com/media/storage_video/26898092.mp4"/>
        <content><![CDATA[The Most Shocking Tables, Ladders & Chairs Moments]]></content>
        <wwe:eventDate>2014-12-09T16:33:00-05:00</wwe:eventDate>
        <wwe:releaseDate>2014-12-09T16:33:00-05:00</wwe:releaseDate>
        <wwe:aspectRatio>
        </wwe:aspectRatio>
        <wwe:hours>0</wwe:hours>
        <wwe:minutes>3</wwe:minutes>
        <wwe:seconds>2</wwe:seconds>
        <wwe:breadCrumb>Homepage#Videos#The-most-shocking-tables-ladders-chairs-moments-26898092</wwe:breadCrumb>
      </entry>
      </feed>

使用的样式表:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet 
    xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:wwe="http://release.wwe.com/atom/1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:strip-space elements="*"/>

    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/">
        <xsl:apply-templates select="/atom:feed/atom:entry[wwe:asset_type='Video']"/>       
    </xsl:template>

    <!-- this template will delete the target nodes -->
    <xsl:template match="/atom:feed/atom:entry[wwe:asset_type='Video']/atom:id|/atom:feed/atom:entry[wwe:asset_type='Video']/atom:updated|/atom:feed/atom:entry[wwe:asset_type='Video']/atom:category"/>

</xsl:stylesheet>

结果:

<entry xmlns="http://www.w3.org/2005/Atom" xmlns:wwe="http://release.wwe.com/atom/1.0">
<title>The Most Shocking Tables, Ladders &amp; Chairs Moments</title>
<category term="Non-Branded"/>
<wwe:asset_type>Video</wwe:asset_type>
<link rel="enclosure" type="image/jpeg" href="http://us.wwe.com/f/video/thumb/2014/12/cdown_tlc_moments_updatedlogo.jpg"/>
<link rel="enclosure" type="video/x-flv" href="http://release.wwe.com/media/storage_video/26898092.flv"/>
<link rel="enclosure" type="video/mp4" href="http://release.wwe.com/media/storage_video/26898092.f4v"/>
<link rel="enclosure" type="video/x-ms-wmv" href="http://release.wwe.com/media/storage_video/26898092.wmv"/>
<link rel="enclosure" type="video/h264" href="http://release.wwe.com/media/storage_video/26898092.mp4"/>
<content>The Most Shocking Tables, Ladders &amp; Chairs Moments</content>
<wwe:eventDate>2014-12-09T16:33:00-05:00</wwe:eventDate>
<wwe:releaseDate>2014-12-09T16:33:00-05:00</wwe:releaseDate>
<wwe:aspectRatio/>
<wwe:hours>0</wwe:hours>
<wwe:minutes>3</wwe:minutes>
<wwe:seconds>2</wwe:seconds>
<wwe:breadCrumb>Homepage#Videos#The-most-shocking-tables-ladders-chairs-moments-26898092</wwe:breadCrumb>
</entry>

我真正需要的是什么:

<rss xmlns="http://www.w3.org/2005/Atom" xmlns:wwe="http://release.wwe.com/atom/1.0">
    <channel>
        <title><![CDATA[WWE Syndication Feed]]></title>
        <link>http://release.wwe.com/wweatomfeed.atom</link>
        <description>WWE Feed to syndicate WWE assets to domestic as well as international partners. This feed has custom modules for accommodating custom data transfer to clients</description>
        <rights>2011 World Wrestling Entertainment!</rights>

        <item>
            <title>The Most Shocking Tables, Ladders &amp; Chairs Moments</title>
            <category term="Non-Branded"/>
            <media:group>
                <media:content type="video/mp4" url="http://release.wwe.com/media/storage_video/26898092.f4v"/>
                <media:content type="image/jpeg" url="http://us.wwe.com/f/video/thumb/2014/12/cdown_tlc_moments_updatedlogo.jpg"/>
            </media:group>
        </item>

        <item>
        .
        .
        .
        .
        .
        .
        </item>
    </channel>
</rss>

2 个答案:

答案 0 :(得分:1)

我认为这个简单的样式表可以解答您的问题:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet 
    xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:wwe="http://release.wwe.com/atom/1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:media="wwww.yourmedia.com"
    exclude-result-prefixes="atom"
    version="1.0">

    <xsl:strip-space elements="*"/>

    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/">
        <rss xmlns="http://www.w3.org/2005/Atom">
            <xsl:element name="title" namespace="http://www.w3.org/2005/Atom"/>
            <xsl:element name="link" namespace="http://www.w3.org/2005/Atom"/>
            <xsl:element name="description" namespace="http://www.w3.org/2005/Atom"/>
            <xsl:for-each select="/atom:feed/atom:entry[*[local-name()='asset_type'][.='Video']]">
                <xsl:element name="item" namespace="http://www.w3.org/2005/Atom">
                    <xsl:apply-templates select="atom:title"/>
                    <xsl:apply-templates select="atom:category"/>
                    <xsl:if test="atom:link">
                        <media:group>
                            <xsl:apply-templates select="atom:link[@type='video/mp4']"/>
                            <xsl:apply-templates select="atom:link[@type='image/jpeg']"/>
                        </media:group>
                    </xsl:if>
                </xsl:element>
            </xsl:for-each>
        </rss>
    </xsl:template>

   <xsl:template match="atom:link">
       <media:content>
           <xsl:apply-templates select="@*[not(name()='rel')]"/>
       </media:content>
   </xsl:template>

</xsl:stylesheet>

答案 1 :(得分:0)

试试这个

    <?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                exclude-result-prefixes="msxsl wwe test atom"
                xmlns:test="http://test.com/atom/1.0"
                xmlns:atom="http://www.w3.org/2005/Atom"
                xmlns:wwe="http://release.wwe.com/atom/1.0"
                version="1.0">
  <xsl:template match="/">
    <good>
      <xsl:for-each select="atom:feed/atom:entry">
        <assettype>
          <xsl:if test="test:asset_type='VIDEO'">
            <xsl:copy-of select="current()"/>
          </xsl:if>
          <xsl:if test="wwe:asset_type='VIDEO'">
            <xsl:copy-of select="current()"/>
          </xsl:if>
          <xsl:if test="test:asset_type='Video'">
            <xsl:copy-of select="current()"/>
          </xsl:if>
          <xsl:if test="wwe:asset_type='Video'">
            <xsl:copy-of select="current()"/>
          </xsl:if>
        </assettype>
      </xsl:for-each>
    </good>
  </xsl:template>
</xsl:stylesheet>

你的xml是无效的,因为“wwe”前缀尚未定义。 这就是为什么我假设通过使用以下命名空间“检查”存在于xml中。 这对于您的xml会有所不同,但您必须相应地更改xslt命名空间部分

相关问题