2012-09-12 80 views
1

我在我的項目中有以下XML sturture。我需要寫的財產以後該調換重複的節點,使扁平stutureXML flaten重複節點

<bookstore> 
    <book > 
    <title lang="en">Everyday Italian</title> 
    <author>Giada De Laurentiis</author> 
    <year>2005</year> 
    <price>30.00</price> 
    </book> 
    <book > 
    <title lang="en">Harry Potter</title> 
    <author>J K. Rowling</author> 
    <year>2005</year> 
    <price>29.99</price> 
    </book> 
    <book> 
    <title lang="en">Learning XML</title> 
    <author>Erik T. Ray</author> 
    <year>2003</year> 
    <price>39.95</price> 
    </book> 
</bookstore> 

我想flaten的sturture這樣

<bookstore> 

    <bookonetitle lang="en">Everyday Italian</bookonetitle> 
    <bookoneauthor>Giada De Laurentiis</bookoneauthor> 
    <bookoneyear>2005</bookoneyear> 
    <bookoneprice>30.00</bookoneprice> 


    <booktwotitle lang="en">Harry Potter</booktwotitle> 
    <booktwoauthor>J K. Rowling</booktwoauthor> 
    <booktwoyear>2005</booktwoyear> 
    <booktwoprice>29.99</booktwoprice> 

    <bookthreetitle lang="en">Learning XML</bookthreetitle> 
    <bookthreetitle>Erik T. Ray</bookthreetitle> 
    <bookthreetitle>2003</bookthreetitle> 
    <bookthreetitle>39.95</bookthreetitle> 



</bookstore> 
+0

它僅限於3?或者可以有任何號碼?很難將數字轉換爲英文拼寫的數字。也許像書名一樣的元素名稱就足夠了? –

+0

XSLT 1.0?或2.0? –

+1

我很好奇你爲什麼要將數據放入元素名稱中,而不是僅僅具有附加屬性的書籍平面結構。重組的目標是什麼? –

回答

3

你輸出結構令我是非常尷尬的。您可能想重新考慮它。但在任何情況下,本XSLT 1.0樣式表...

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

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

<xsl:template match="book"> 
    <xsl:apply-templates select="node()"/> 
</xsl:template> 

<xsl:template match="book/*"> 
    <xsl:element name="book-{count(../preceding-sibling::book)+1}-{local-name()}"> 
    <xsl:apply-templates select="@*|node()"/> 
    </xsl:element> 
</xsl:template> 

</xsl:stylesheet> 

...將改變你的樣品輸入...

<bookstore> 
    <book-1-title lang="en">Everyday Italian</book-1-title> 
    <book-1-author>Giada De Laurentiis</book-1-author> 
    <book-1-year>2005</book-1-year> 
    <book-1-price>30.00</book-1-price> 
    <book-2-title lang="en">Harry Potter</book-2-title> 
    <book-2-author>J K. Rowling</book-2-author> 
    <book-2-year>2005</book-2-year> 
    <book-2-price>29.99</book-2-price> 
    <book-3-title lang="en">Learning XML</book-3-title> 
    <book-3-author>Erik T. Ray</book-3-author> 
    <book-3-year>2003</book-3-year> 
    <book-3-price>39.95</book-3-price> 
</bookstore> 
+4

我同意其他人的想法,這是一件很奇怪的事情。但是,如果你堅持......如果你想在輸出中輸入數字「1」,「2」,「3」而不是1,2,3,那麼在XSLT 2.0中'會爲你做這項工作。 –

+0

這很奇怪,但我們使用的導入工具不支持子節點。非常奇怪我同意。 –

1

按照邁克爾·凱的評論,你最好選項是使用XSLT 2.0指令:

<xsl:number value="$n" format="w"/> 

但是,如果您不能使用XSLT 2.0和書的數量是有限的和事先知道了極限,然後搜索解決方案Ñ這樣可以使用:

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

<my:numbers> 
    <num>one</num> 
    <num>two</num> 
    <num>three</num> 
    <num>four</num> 
    <num>five</num> 
    <num>six</num> 
    <num>seven</num> 
    <num>eight</num> 
    <num>nine</num> 
    <num>ten</num> 
    <num>eleven</num> 
    <num>twelve</num> 
</my:numbers> 

<xsl:variable name="vNumbers" select="document('')/*/my:numbers/*"/> 

<xsl:template match="/*"> 
    <bookstore><xsl:apply-templates/></bookstore> 
</xsl:template> 
<xsl:template match="book"> 
    <xsl:apply-templates select="*"> 
    <xsl:with-param name="pPos" select="position()"/> 
    </xsl:apply-templates> 
    <xsl:text>&#xA;</xsl:text> 
</xsl:template> 

<xsl:template match="book/*"> 
    <xsl:param name="pPos"/> 

    <xsl:element name="book{$vNumbers[$pPos]}{name()}"> 
    <xsl:copy-of select="@*|node()"/> 
    </xsl:element> 
</xsl:template> 
</xsl:stylesheet> 

當這個變換所提供的XML文檔施加:

<bookstore> 
    <book > 
    <title lang="en">Everyday Italian</title> 
    <author>Giada De Laurentiis</author> 
    <year>2005</year> 
    <price>30.00</price> 
    </book> 
    <book > 
    <title lang="en">Harry Potter</title> 
    <author>J K. Rowling</author> 
    <year>2005</year> 
    <price>29.99</price> 
    </book> 
    <book> 
    <title lang="en">Learning XML</title> 
    <author>Erik T. Ray</author> 
    <year>2003</year> 
    <price>39.95</price> 
    </book> 
</bookstore> 

有用,正確的結果產生

<bookstore> 
    <bookonetitle lang="en">Everyday Italian</bookonetitle> 
    <bookoneauthor>Giada De Laurentiis</bookoneauthor> 
    <bookoneyear>2005</bookoneyear> 
    <bookoneprice>30.00</bookoneprice> 

    <booktwotitle lang="en">Harry Potter</booktwotitle> 
    <booktwoauthor>J K. Rowling</booktwoauthor> 
    <booktwoyear>2005</booktwoyear> 
    <booktwoprice>29.99</booktwoprice> 

    <bookthreetitle lang="en">Learning XML</bookthreetitle> 
    <bookthreeauthor>Erik T. Ray</bookthreeauthor> 
    <bookthreeyear>2003</bookthreeyear> 
    <bookthreeprice>39.95</bookthreeprice> 

</bookstore>