格式化xsl模板

时间:2011-02-22 18:30:52

标签: visual-studio-2008 xslt

我刚刚开始让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>

每个文件中有多个条目     

感谢任何帮助或指向优秀教程的链接。如果我需要在每个元素中,也想学习编辑值。

EDIT ::

好的想出了一个部分,但我没有得到所有的条目值只是第一个;

<?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 doc并使用旧xml中的条目值映射到新的xml文档。我很欣赏发布的代码,但我如何采用它来做我想做的事。

1 个答案:

答案 0 :(得分:2)

此样式表:

<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>

注意:纯拉风格。 修改:在包含可选元素的大型文档中,最好不要使用文本节点内置规则。

相关问题