XSL XML到CSV位置编号 [英] XSL XML to CSV position number
问题描述
我的新手在XSL我有一个xml文件像上面,我想把它转换为csv我使用java main执行它,但我有一个问题,在xsl文件geting位置号码
I'am newbie in XSL i have an xml file like above and i would like to transform it to csv i use java main to execute it but i has a problem with geting position number in xsl file
<parent>
<child name="a" type="1"/>
<child name="b" type="2"/>
<child name="c" type="1"/>
<child name="d" type="3"/>
</parent>
输出为:
a 1
b 2
c 1
d 3
但我想得到的是:
child name type
1 a 1
2 b 2
3 c 1
4 d 3
应该是子位置
这是我的xsl文件
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="child">
<!--header row-->
<xsl:for-each select="child">
<xsl:number value="position()" format="1" />
<xsl:apply-templates />
<xsl:text> </xsl:text>
</xsl:for-each>
<xsl:for-each select="@*">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">
<xsl:value-of select="';'"/>
</xsl:if>
</xsl:for-each>
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>
所以总结一下我有两个问题如何添加标题行像示例一样如何获取位置
So to summarize i have two problem how can add header row like the example and how can get position of child and add it
正如其他答案中所指出的,你的主要问题是上下文:你必须在上下文中<$ class =
c $ c> parent 以执行< xsl:for-each select =child>
。
As noted in other answers, your main problem is context: you must be in the context of parent
in order to do <xsl:for-each select="child">
.
如果您希望这是动态的,包括标题行,请尝试:
If you want this to be dynamic, including the header row, try:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/*">
<!-- header row-->
<xsl:for-each select="*[1] | *[1]/@*">
<xsl:value-of select="local-name()"/>
<xsl:if test="position() != last()">
<xsl:text>;</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text> </xsl:text>
<!-- body -->
<xsl:for-each select="*">
<xsl:number/>
<xsl:text>;</xsl:text>
<xsl:for-each select="@*">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">
<xsl:text>;</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
这篇关于XSL XML到CSV位置编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!