Question

I'm importing XML data in filemaker and I need a little help for formatting data.

My XML file:

<?xml version="1.0" standalone="yes"?>
<articoli>
  <articolo>
    <Codice>000001101</Codice>
    <prezzoPers>12.20</prezzoPers>
    <data>20130916</data>
  </articolo>
</articoli>

My XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


    <xsl:template match="/*">
        <FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
            <ERRORCODE>0</ERRORCODE>
            <PRODUCT BUILD="" NAME="" VERSION=""/>
            <DATABASE DATEFORMAT="yyyymmdd" LAYOUT="" NAME="" RECORDS="{count(/*/*)}" TIMEFORMAT="h:mm:ss a"/>
            <METADATA>
                 <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="Codice" TYPE="NUMBER"/>
                 <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="prezzoPers" TYPE="NUMBER"/>
                 <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="data" TYPE="DATE"/>

            </METADATA>
            <RESULTSET>
                <xsl:attribute name="FOUND"><xsl:value-of select="count(child::*)"/></xsl:attribute>
                <xsl:for-each select="child::*">
                    <ROW>
                        <xsl:attribute name="MODID">0</xsl:attribute>
                        <xsl:attribute name="RECORDID">0</xsl:attribute>
                        <xsl:for-each select="child::*">
                            <COL>
                                <DATA>
                                    <xsl:value-of select="."/>
                                </DATA>
                            </COL>
                        </xsl:for-each>
                    </ROW>
                </xsl:for-each>
            </RESULTSET>
        </FMPXMLRESULT>
    </xsl:template>
</xsl:stylesheet>

The import works fine but I would like to format the field "data" from yyyymmdd to dd/mm/yyyy and in the field "prezzoPers" use the "," instead of "." I'm a novice with XSLT so if someone can give me a little help I'll appreciate! Thanks in advice. Walter

Was it helpful?

Solution

You can do that using the next XSLT:

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

    <xsl:template match="/articoli">
        <FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
            <ERRORCODE>0</ERRORCODE>
            <PRODUCT BUILD="" NAME="" VERSION=""/>
            <DATABASE DATEFORMAT="yyyymmdd" LAYOUT="" NAME="" RECORDS="{count(articolo)}" TIMEFORMAT="h:mm:ss a"/>
            <METADATA>
                <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="Codice" TYPE="NUMBER"/>
                <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="prezzoPers" TYPE="NUMBER"/>
                <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="data" TYPE="DATE"/>
            </METADATA>
            <RESULTSET FOUND="{count(articolo)}">
                <xsl:for-each select="articolo">
                    <ROW MODID="0" RECORDID="0">
                        <xsl:for-each select="child::*">
                            <COL>
                                <DATA>
                                    <xsl:choose>
                                        <xsl:when test="local-name() = 'prezzoPers'">
                                            <!-- Will be performed on element 'prezzoPers' -->
                                            <xsl:value-of select="translate(., '.', ',')" />
                                        </xsl:when>
                                        <xsl:when test="local-name() = 'data'">
                                            <!-- Will be performed on element 'data' -->
                                            <xsl:value-of select="substring(., '7', '2')" /><xsl:value-of select="substring(., '5', '2')" /><xsl:value-of select="substring(., '1', '4')" />
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <!-- Will be performed on element 'Codice' -->
                                                    <xsl:value-of select="."/>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </DATA>
                            </COL>
                        </xsl:for-each>
                    </ROW>
                </xsl:for-each>
            </RESULTSET>
        </FMPXMLRESULT>
    </xsl:template>
</xsl:stylesheet>

OTHER TIPS

Search for the functions substring-before and substring-after or substring for getting parts of an existing string to rebuild it. Search for the function transform for changing characters in a string to another.

That should get you started in doing your own attempt.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top