2

Convert the XML into CSV using XSLT transformation

<start>
<article>
<key>key1</key>

<definition type="m">
<![CDATA[abcdefghijk]]>
</definition>
</article>

<article>
<key>key2</key>

<definition type="m">
<![CDATA[bcdefghijkl]]>
</definition>
</article>
</start>

csv will look like

key1,abcdefghijk
key2,bcdefghijkl

I'v learn w3c school xslt tutorial,but can't get practice. Can someone write XSLT code for transformation?

1

2 Answers 2

1

You can achieve the wanted result with a single template:

<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="start/article">
        <xsl:if test="position()>1">
            <xsl:text>&#xA;</xsl:text>
        </xsl:if>
        <xsl:value-of select="normalize-space(
            concat(key,'; ',definition))"/>
    </xsl:template>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

Comments

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

  <xsl:template match="/">
    <xsl:apply-templates select="//article/*"/>
  </xsl:template>

  <xsl:template match="key">
    <xsl:copy-of select="normalize-space(concat(., ';'))"/>
  </xsl:template>

  <xsl:template match="definition">
    <xsl:copy-of select="normalize-space(.)"/>
    <xsl:text>&#xD;&#xA;</xsl:text>
  </xsl:template>

</xsl:stylesheet>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.