formatting xsl template
I just started to get the xsl to xml matching correct but I am not sure how to loop through each entry in the xml and get the entry values I need. my xsl template code
xmlns:x="http://www.w3.org/2005/Atom" >
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:element name="Post">
<xsl:copy>
<xsl:for-each select="*">
<xsl:element name="siteid">
<xsl:value-of select="substring(x:entry/x:id,29)"/>
</xsl:element>
<xsl:element name="date">
<xsl:value-of select="x:entry/x:published"/>
</xsl:element>
<xsl:element name="sitetitle">
<xsl:value-of select="x:entry/x:title"/>
</xsl:element>
<xsl:element name="content">
<xsl:value-of select="x:entry/x:summary"/>
</xsl:element>
<xsl:element name="author">
<xsl:value-of select="x:entry/x:author/x:name"/>
</xsl:element>
<xsl:element name="authorurl">
<xsl:value-of select="x:entry/x:author/x:uri"/>
</xsl:element>
</xsl:for-each>
</xsl:copy>
</xsl:element>
</xsl:template>
There are more elements present.. The xml file sample contains:
<Results>
<entry xmlns:gnip="http://www.text.com/schemas/
2010" xmlns="http://www.w3.org/2005/Atom">
<id>tag:search.api.com,2005:38704434133471232</id>
<published>2011-02-18T21:00:30Z</published>
<updated>2011-02-18T21:00:30Z</updated>
<title>QVT (The QVT)</title>
<summary type="html">http://www.guardian.co.uk/business/
2011/feb/开发者_StackOverflow社区18/barclays-bank-113m-corporation-tax</summary>
.....
</entry>
Multiple entries in each file with root
Appreciate any help or links to good tutorials. Also want to learn to edit the values if I need to within each element.
EDIT::
Ok figured out one part but I am not getting all the entry values just the first;
<?xml version="1.0" encoding="utf-8"?>
<Results>
<postid>38704434133471232</postid>
<siteid />
<sitetitle />
<forumid />
<forumname />
<forumurl />
<internalthreadid />
<ThreadID />
<Url />
<MainUrl />
<content>ttp://www.guardian.co.uk/business/
2011/feb/18/barclays-bank-113m-corporation-tax</content>
<date>2011-02-18T21:00:30Z</date>
<title>QVT (The QVT) posted a note on Twitter</title>
I am creating a new xml doc and using the entry values from the old xml to map to new one. I appreciate the code posted but how do I adopt it to do what I want it to do.
This stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://www.w3.org/2005/Atom"
exclude-result-prefixes="x">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Results">
<Transformation>
<xsl:apply-templates/>
</Transformation>
</xsl:template>
<xsl:template match="x:entry">
<Post>
<xsl:apply-templates/>
</Post>
</xsl:template>
<xsl:template match="x:entry/x:id">
<siteid>
<xsl:value-of select="substring(.,29)"/>
</siteid>
</xsl:template>
<xsl:template match="x:entry/x:published">
<date>
<xsl:value-of select="."/>
</date>
</xsl:template>
<xsl:template match="x:entry/x:title">
<sitetitle>
<xsl:value-of select="."/>
</sitetitle>
</xsl:template>
<xsl:template match="x:entry/x:summary">
<content>
<xsl:value-of select="."/>
</content>
</xsl:template>
<xsl:template match="x:entry/x:author/x:name">
<author>
<xsl:value-of select="."/>
</author>
</xsl:template>
<xsl:template match="x:entry/x:author/x:uri">
<authorurl>
<xsl:value-of select="."/>
</authorurl>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
With this input:
<Results>
<entry xmlns:gnip="http://www.text.com/schemas/2010"
xmlns="http://www.w3.org/2005/Atom">
<id>tag:search.api.com,2005:38704434133471232</id>
<published>2011-02-18T21:00:30Z</published>
<updated>2011-02-18T21:00:30Z</updated>
<title>QVT (The QVT)</title>
<summary type="html">http://www.guardian.co.uk/business/2011/feb/18/barclays-bank-113m-corporation-tax</summary>
</entry>
<entry xmlns:gnip="http://www.text.com/schemas/2010"
xmlns="http://www.w3.org/2005/Atom">
<id>other</id>
<published>today</published>
<updated>today</updated>
<title>Test</title>
<summary type="html">Test record</summary>
</entry>
</Results>
Output:
<Transformation>
<Post>
<siteid>4434133471232</siteid>
<date>2011-02-18T21:00:30Z</date>
<sitetitle>QVT (The QVT)</sitetitle>
<content>http://www.guardian.co.uk/business/2011/feb/18/barclays-bank-113m-corporation-tax</content>
</Post>
<Post>
<siteid></siteid>
<date>today</date>
<sitetitle>Test</sitetitle>
<content>Test record</content>
</Post>
</Transformation>
Note: Pure pull style. Edit: In large documents with optional elements it's better not to use text nodes built-in rule.
精彩评论