Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

Parsing string inside XML element into CSV using XSLT

I'm new to XSLT and would appreciate some help on issue below:

I have XML including concatenated info about packages. I would need to parse them into CSV using XSLT (separator would be semicolon). I would need to have separate lines generated for all concatenated items, so some for-each - looping is needed also.

The XML looks like this:

<Package>
    <Content>Goodies</Content>
    <Weight>TotalWeight</Weight>
    <Dimensions>Lenght1/Width1/Height1/Weight1,Lenght2/Width2/Height2/Weight2</Dimensions>
    <PackingType>Pallet</PackingType>
</Package>

And the desired output would look like this:

1;Goodies;TotalWeight;Lenght1;Width1;Height1;Weight1;Pallet 2;Goodies;TotalWeight;Lenght2;Width2;Height2;Weight2;Pallet

And this should be done using XSLT 1.0 (yep, we have some limitations).

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try something like this:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

  <xsl:template match="Package">
    <xsl:call-template name="row">
      <xsl:with-param name="pack" select="."/>
      <xsl:with-param name="str" select="Dimensions"/>
      <xsl:with-param name="nn" select="1"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="row">
    <xsl:param name="pack"/>
    <xsl:param name="str"/>
    <xsl:param name="nn"/>
    <xsl:variable name="s1" select="substring-before($str, ',')"/>
    <xsl:if test="$s1">
      <xsl:variable name="s2" select="substring-after($str, ',')"/>
      <xsl:call-template name="rowInternal">
        <xsl:with-param name="pack" select="$pack"/>
        <xsl:with-param name="str" select="$s1"/>
        <xsl:with-param name="nn" select="$nn"/>
      </xsl:call-template>
      <xsl:call-template name="row">
        <xsl:with-param name="pack" select="$pack"/>
        <xsl:with-param name="str" select="$s2"/>
        <xsl:with-param name="nn" select="$nn+1"/>
      </xsl:call-template>
    </xsl:if>
    <xsl:if test="not($s1) and $str">
      <xsl:call-template name="rowInternal">
        <xsl:with-param name="pack" select="$pack"/>
        <xsl:with-param name="str" select="$str"/>
        <xsl:with-param name="nn" select="$nn"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template name="rowInternal">
    <xsl:param name="pack"/>
    <xsl:param name="str"/>
    <xsl:param name="nn"/>
    <xsl:value-of select="$nn"/>
    <xsl:text>;</xsl:text>
    <xsl:value-of select="$pack/Content"/>
    <xsl:text>;</xsl:text>
    <xsl:value-of select="$pack/Weight"/>
    <xsl:text>;</xsl:text>
    <xsl:value-of select="translate($str, '/', ';')"/>
    <xsl:text>;</xsl:text>
    <xsl:value-of select="$pack/PackingType"/>
    <xsl:text>&#xA;</xsl:text>
  </xsl:template>

</xsl:transform>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...