2011-02-22 87 views
0

我剛開始得到xsl到xml匹配正確,但我不知道如何遍歷xml中的每個條目並獲取我需要的條目值。 我XSL模板代碼格式化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文檔。我欣賞發佈的代碼,但我如何採用它來做我想做的事情。

回答

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> 

注意:純拉風。 編輯:在具有可選元素的大文檔中,最好不要使用文本節點內置規則。

+0

謝謝你非常有用,但我實際上只需要提取某些元素並聲明新元素。 – vbNewbie 2011-02-22 20:18:59