Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...