2012-12-29 46 views
1

我在這裏發現了一些代碼,這是一個不錯的開始,但它與我正在尋找的有點不同,我不知道如何繼續。我想要做的是複製整個XML文件,通過分割基於多個分隔符的Author元素創建新元素,並刪除原始的「Author」元素名稱。每個作者「set」用分號分隔,並以記錄4中的回車或回車結束。任何幫助將不勝感激。我是一名XSLT新手,對於提供的解決方案的任何解釋都會很好,因爲我想了解該解決方案,而不僅僅是使其工作。謝謝。XSLT根據多個分隔符將元素拆分爲新元素

的代碼:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:e="http://localhost"> 
<xsl:output indent="yes"/> 
<e:e>AuthorName</e:e> 
<e:e>AuthorLocation</e:e> 
<e:e>AuthorPhone</e:e> 
<xsl:variable name="vElement" select="document('')/*/e:*"/> 
<xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 
<xsl:template match="Author/text()" name="tokenizer"> 
    <xsl:param name="pString" select="string()"/> 
    <xsl:param name="pPosition" select="1"/> 
    <xsl:if test="$pString"> 
     <xsl:element name="{$vElement[$pPosition]}"> 
      <xsl:value-of select="normalize-space(substring-before(concat($pString,';'),';'))"/> 
     </xsl:element> 
     <xsl:call-template name="tokenizer"> 
      <xsl:with-param name="pString" select="substring-after($pString,';')"/> 
      <xsl:with-param name="pPosition" select="$pPosition + 1"/> 
     </xsl:call-template> 
    </xsl:if> 
</xsl:template> 

XML文件

<RECORD_SET> 
    <RECORD NUM="1"> 
     <Title>Title1</Title> 
     <Author>Author Name 1; Author Location 1; Author Phone Number 1</Author> 
     <Date>11/20/1976</Date> 
    </RECORD> 
    <RECORD NUM="2"> 
     <Title>Title2</Title> 
     <Author>Author Name 2; Author Location 2; Author Phone Number 2 
     Author Name 3; Author Location 3; Author Phone Number 3 
     Author Name 4; Author Location 4; Author Phone Number 4</Author> 
     <Date>10/20/2001</Date> 
    </RECORD> 
    <RECORD NUM="3"> 
     <Title>Title3</Title> 
     <Author>Author Name 5; Author Location 5; Author Phone Number 5 
     Author Name 6; Author Location 6; Author Phone Number 6</Author> 
     <Date>09/18/1966</Date> 
    </RECORD> 
    <RECORD NUM="4"> 
     <Title>Title4</Title> 
     <Author>Author Name 7 
     Author Name 8</Author> 
     <Date>01/18/1956</Date> 
    </RECORD> 
</RECORD_SET> 

希望的輸出

<RECORD_SET> 
    <RECORD NUM="1"> 
     <Title>Title1</Title> 
     <AuthorName>Author Name 1</Author> 
     <AuthorLocation>Author Location 1</AuthorLocation> 
     <AuthorPhone>Author Phone Number 1</AuthorPhone> 
     <Date>11/20/1976</Date> 
    </RECORD> 
    <RECORD NUM="2"> 
     <Title>Title2</Title> 
     <AuthorName>Author Name 2</Author> 
     <AuthorLocation>Author Location 2</AuthorLocation> 
     <AuthorPhone>Author Phone Number 2</AuthorPhone> 
     <AuthorName>Author Name 3</Author> 
     <AuthorLocation>Author Location 3</AuthorLocation> 
     <AuthorPhone>Author Phone Number 3</AuthorPhone> 
     <AuthorName>Author Name 4</Author> 
     <AuthorLocation>Author Location 4</AuthorLocation> 
     <AuthorPhone>Author Phone Number 4</AuthorPhone> 
     <Date>10/20/2001</Date> 
    </RECORD> 
    <RECORD NUM="3"> 
     <Title>Title3</Title> 
     <AuthorName>Author Name 5</Author> 
     <AuthorLocation>Author Location 5</AuthorLocation> 
     <AuthorPhone>Author Phone Number 5</AuthorPhone> 
     <AuthorName>Author Name 6</Author> 
     <AuthorLocation>Author Location 6</AuthorLocation> 
     <AuthorPhone>Author Phone Number 6</AuthorPhone> 
     <Date>09/18/1966</Date> 
    </RECORD> 
    <RECORD NUM="4"> 
     <Title>Title4</Title> 
     <AuthorName>Author Name 7</AuthorName> 
     <AuthorName>Author Name 8</AuthorName> 
     <Date>01/18/1956</Date> 
    </RECORD> 
</RECORD_SET> 

電流輸出

<RECORD NUM="1"> 
    <Title>Title1</Title> 
    <Author> 
     <AuthorName>Author Name 1</AuthorName> 
     <AuthorLocation>Author Location 1</AuthorLocation> 
     <AuthorPhone>Author Phone Number 1</AuthorPhone> 
    </Author> 
    <Date>11/20/1976</Date> 
</RECORD> 
<RECORD NUM="2"> 
    <Title>Title2</Title> 
    <Author> 
     <AuthorName>Author Name 2</AuthorName> 
     <AuthorLocation>Author Location 2</AuthorLocation> 
     <AuthorPhone>Author Phone Number 2 Author Name 3</AuthorPhone>Author Location 3Author Phone Number 3 Author Name 4Author Location 4Author Phone Number 4 
    </Author> 
    <Date>10/20/2001</Date> 
</RECORD> 
<RECORD NUM="3"> 
    <Title>Title3</Title> 
    <Author> 
     <AuthorName>Author Name 5</AuthorName> 
     <AuthorLocation>Author Location 5</AuthorLocation> 
     <AuthorPhone>Author Phone Number 5 Author Name 6</AuthorPhone>Author Location 6Author Phone Number 6 
    </Author> 
    <Date>09/18/1966</Date> 
</RECORD> 
<RECORD NUM="4"> 
    <Title>Title4</Title> 
    <Author> 
     <AuthorName>Author Name 7 Author Name 8</AuthorName> 
    </Author> 
    <Date>01/18/1956</Date> 
</RECORD> 

+0

什麼是現在的產出? – PCM

回答

0

此XSLT 2.0轉化

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:variable name="vAuthorDataNames" select= 
    "'AuthorName', 'AuthorLocation', 'AuthorPhone'"/> 

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

<xsl:template match="Author"> 
    <xsl:variable name="vAuthors" select="tokenize(., '(\n(\r?))\s*')"/> 

    <xsl:for-each select="$vAuthors[normalize-space(.)]"> 
    <xsl:variable name="vAuthorData" select="tokenize(., ';\s*')"/> 

    <xsl:for-each select="$vAuthorData"> 
    <xsl:variable name="vPos" select="position()"/> 

    <xsl:element name="{$vAuthorDataNames[$vPos]}"> 
    <xsl:value-of select="."/> 
    </xsl:element> 
    </xsl:for-each> 
    </xsl:for-each> 
</xsl:template> 
</xsl:stylesheet> 

當所提供的XML文檔應用:

<RECORD_SET> 
    <RECORD NUM="1"> 
     <Title>Title1</Title> 
     <Author>Author Name 1; Author Location 1; Author Phone Number 1</Author> 
     <Date>11/20/1976</Date> 
    </RECORD> 
    <RECORD NUM="2"> 
     <Title>Title2</Title> 
     <Author>Author Name 2; Author Location 2; Author Phone Number 2 
     Author Name 3; Author Location 3; Author Phone Number 3 
     Author Name 4; Author Location 4; Author Phone Number 4</Author> 
     <Date>10/20/2001</Date> 
    </RECORD> 
    <RECORD NUM="3"> 
     <Title>Title3</Title> 
     <Author>Author Name 5; Author Location 5; Author Phone Number 5 
     Author Name 6; Author Location 6; Author Phone Number 6</Author> 
     <Date>09/18/1966</Date> 
    </RECORD> 
    <RECORD NUM="4"> 
     <Title>Title4</Title> 
     <Author>Author Name 7 
     Author Name 8</Author> 
     <Date>01/18/1956</Date> 
    </RECORD> 
</RECORD_SET> 

產生想要的,正確的結果:

<RECORD_SET> 
    <RECORD NUM="1"> 
     <Title>Title1</Title> 
     <AuthorName>Author Name 1</AuthorName> 
     <AuthorLocation>Author Location 1</AuthorLocation> 
     <AuthorPhone>Author Phone Number 1</AuthorPhone> 
     <Date>11/20/1976</Date> 
    </RECORD> 
    <RECORD NUM="2"> 
     <Title>Title2</Title> 
     <AuthorName>Author Name 2</AuthorName> 
     <AuthorLocation>Author Location 2</AuthorLocation> 
     <AuthorPhone>Author Phone Number 2</AuthorPhone> 
     <AuthorName>Author Name 3</AuthorName> 
     <AuthorLocation>Author Location 3</AuthorLocation> 
     <AuthorPhone>Author Phone Number 3</AuthorPhone> 
     <AuthorName>Author Name 4</AuthorName> 
     <AuthorLocation>Author Location 4</AuthorLocation> 
     <AuthorPhone>Author Phone Number 4</AuthorPhone> 
     <Date>10/20/2001</Date> 
    </RECORD> 
    <RECORD NUM="3"> 
     <Title>Title3</Title> 
     <AuthorName>Author Name 5</AuthorName> 
     <AuthorLocation>Author Location 5</AuthorLocation> 
     <AuthorPhone>Author Phone Number 5</AuthorPhone> 
     <AuthorName>Author Name 6</AuthorName> 
     <AuthorLocation>Author Location 6</AuthorLocation> 
     <AuthorPhone>Author Phone Number 6</AuthorPhone> 
     <Date>09/18/1966</Date> 
    </RECORD> 
    <RECORD NUM="4"> 
     <Title>Title4</Title> 
     <AuthorName>Author Name 7</AuthorName> 
     <AuthorName>Author Name 8</AuthorName> 
     <Date>01/18/1956</Date> 
    </RECORD> 
</RECORD_SET> 

說明

正確使用的satndard XSLT功能tokenize()normalize-space()

+0

謝謝Dimitre。我很抱歉沒有在我原來的問題中指定它,但我使用的是XSLT 1.0。 –

+1

@HammerTime,使用XSLT 1.0,可以將此XSLT 2.0解決方案機械地轉換爲XSLT 1.0。或者使用FXSL的str-split-words模板。或者編寫自己的遞歸解決方案 - 這將是5倍更大,難以維護。切換到XSLT 2.0 ... –