2012-09-12 79 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>