我刚开始使xsl到xml的匹配正确,但是我不确定如何遍历xml中的每个条目并获得所需的入口值。我的xsl模板代码
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>有更多的元素存在..xml文件示例包含:
<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>每个文件中有多个根目录条目
感谢任何帮助或链接到好的教程。我还想学习编辑的值,如果我需要在每个元素。
编辑::
好的,我算出了一个部分,但我没有得到所有的入口值;
<?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>我正在创建一个新的xml文档,并使用旧xml中的条目值来映射到新的xml文档。我很欣赏张贴的代码,但是我如何采用它来做我想要它做的事情呢?
发布于 2011-02-23 04:03:49
此样式表:
<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>使用此输入:
<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>输出:
<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>注释:纯拉入风格。编辑:在包含可选元素的大型文档中,最好不要使用文本节点内置规则。
https://stackoverflow.com/questions/5082247
复制相似问题