XSL XML to CSV position number

2019-07-11 19:04发布

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>

The output is :

a     1
b     2
c     1
d     3

but what i would like to get is :

 child name type
     1    a     1 
     2    b     2
     3    c     1
     4    d     3

with the first column should be the child position

this is my xsl file

<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>&#10;</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>&#10;</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

标签: java xml csv xslt
3条回答
Rolldiameter
2楼-- · 2019-07-11 19:34

You better match the parent, print the header row, then loop over the children:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="parent">
 <!--header row-->
    <xsl:text>child;name;type&#10;</xsl:text>
    <xsl:for-each select="child">
        <xsl:number value="position()" format="1" />
        <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>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>
查看更多
时光不老,我们不散
3楼-- · 2019-07-11 19:51

You can use concat() as below:

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

    <xsl:template match="/*">
        <xsl:value-of select="'child;name;type'"/>
        <xsl:text>&#10;</xsl:text>
        <xsl:apply-templates select="*"/>
    </xsl:template>
    <xsl:template match="child">
        <xsl:value-of select="concat(position(), ';',@name, ';', @type)"/>
        <xsl:if test="position() != last()">
            <xsl:text>&#10;</xsl:text>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>
查看更多
\"骚年 ilove
4楼-- · 2019-07-11 19:53

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>&#10;</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>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>
查看更多
登录 后发表回答