使用xslt将CSV转换为XML – 如何使用递增列名称

前端之家收集整理的这篇文章主要介绍了使用xslt将CSV转换为XML – 如何使用递增列名称前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个xslt将csv转换为xml,工作正常,但所有列的标签都相同.
我需要它像这样增加
  1. <row>
  2. <column1></column1>
  3. <column2></column2>
  4. <column3></column3>
  5. </row>

当我使用position()时,它将所有列重命名为column1

  1. <xsl:element name="{concat('column',position())}">

这是xslt:

  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  2. <xsl:output method="xml" omit-xml-declaration="yes"/>
  3. <xsl:variable name="LF" select="'&#xA;'"/>
  4. <!-- template that matches the root node-->
  5. <xsl:template match="/">
  6. <root>
  7. <xsl:call-template name="texttorows">
  8. <xsl:with-param name="StringToTransform" select="/root"/>
  9. </xsl:call-template>
  10. </root>
  11. </xsl:template>
  12. <!-- template that actually does the conversion-->
  13. <xsl:template name="texttorows">
  14. <!-- import $StringToTransform-->
  15. <xsl:param name="StringToTransform" select="''"/>
  16. <xsl:choose>
  17. <!-- string contains lineFeed-->
  18. <xsl:when test="contains($StringToTransform,$LF)">
  19. <!-- Get everything up to the first carriage return-->
  20. <row>
  21. <xsl:call-template name="csvtoxml">
  22. <xsl:with-param name="StringToTransform" select="substring-before($StringToTransform,$LF)"/>
  23. </xsl:call-template>
  24. </row>
  25. <!-- repeat for the remainder of the original string-->
  26. <xsl:call-template name="texttorows">
  27. <xsl:with-param name="StringToTransform">
  28. <xsl:value-of select="substring-after($StringToTransform,$LF)"/>
  29. </xsl:with-param>
  30. </xsl:call-template>
  31. </xsl:when>
  32. <!-- string does not contain newline,so just output it-->
  33. <xsl:otherwise>
  34. <row>
  35. <xsl:call-template name="csvtoxml">
  36. <xsl:with-param name="StringToTransform" select="$StringToTransform"/>
  37. </xsl:call-template>
  38. </row>
  39. </xsl:otherwise>
  40. </xsl:choose>
  41. </xsl:template>
  42. <xsl:template name="csvtoxml">
  43. <!-- import $StringToTransform-->
  44. <xsl:param name="StringToTransform" select="''"/>
  45. <xsl:choose>
  46. <!-- string contains lineFeed-->
  47. <xsl:when test="contains($StringToTransform,',')">
  48. <!-- Get everything up to the first carriage return-->
  49. <xsl:element name="{concat('column',position())}">
  50. <xsl:value-of select="substring-before($StringToTransform,')"/>
  51. </xsl:element>
  52. <!-- repeat for the remainder of the original string-->
  53. <xsl:call-template name="csvtoxml">
  54. <xsl:with-param name="StringToTransform">
  55. <xsl:value-of select="substring-after($StringToTransform,')"/>
  56. </xsl:with-param>
  57. </xsl:call-template>
  58. </xsl:when>
  59. <!-- string does not contain newline,so just output it-->
  60. <xsl:otherwise>
  61. <column>
  62. <xsl:value-of select="$StringToTransform"/>
  63. </column>
  64. </xsl:otherwise>
  65. </xsl:choose>
  66. </xsl:template>
  67. </xsl:stylesheet>

这是一个示例csv:

  1. <root>
  2. 3779490,916705,CS,60,34.89,Sauce/Cholula
  3. 5918104,918958,6,20.63,Pasta/Fresh/Cavatelli/6#/Frozen
  4. 5064774,920723,10,45.5,Cheese/Oaxaca
  5. 3422752,925230,EA,8,69.6,Chipotle/Powder/Ground
  6. 5955640,BB171,30,50.7,Butter/Unsalted
  7. 5295326,BC110005,6000,54.95,Oil/Olive/Finishing
  8. </root>
看起来csvtoxml被一个大字符串调用,它递归地通过该字符串. position()在这种情况下不起作用,因为你没有使用一组节点.

相反,你可以用计数参数来实现你所追求的目标:

  1. <xsl:template name="csvtoxml">
  2. <!-- import $StringToTransform-->
  3. <xsl:param name="StringToTransform" select="''"/>
  4. <xsl:param name="ColumnNum" select="1"/>
  5. <xsl:choose>
  6. <!-- string contains lineFeed-->
  7. <xsl:when test="contains($StringToTransform,')">
  8. <!-- Get everything up to the first carriage return-->
  9. <xsl:element name="{concat('column',$ColumnNum)}">
  10. <xsl:value-of select="substring-before($StringToTransform,')"/>
  11. </xsl:element>
  12. <!-- repeat for the remainder of the original string-->
  13. <xsl:call-template name="csvtoxml">
  14. <xsl:with-param name="StringToTransform" select="substring-after($StringToTransform,')" />
  15. <xsl:with-param name="ColumnNum" select="$ColumnNum + 1" />
  16. </xsl:call-template>
  17. </xsl:when>
  18. <!-- string does not contain newline,so just output it-->
  19. <xsl:otherwise>
  20. <xsl:element name="{concat('column',$ColumnNum)}">
  21. <xsl:value-of select="$StringToTransform" />
  22. </xsl:element>
  23. </xsl:otherwise>
  24. </xsl:choose>
  25. </xsl:template>

猜你在找的XML相关文章