xmlxsltattributesfilemaker

How to specify XSL attribute only for specific element in XML for a Filemaker extract


I'm trying to build an xml file from data in a Filemaker database. The basic structure of the XML using a XSL is ok but I am having one issue with adding an attribute only for a specific element. The matchmode="0" should only be applied to the "ref" element and not the "date".

This is the XML I get:

    <root>
    <transportbookings>
    <transportbooking>
    <ref matchmode="0">260458</ref>          *! ok*
    <date matchmode="0">13-6-2023</date>     *! not ok*
    </transportbooking>
    </transportbookings>
    </root>

This is the XLS I created:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fm="http://www.filemaker.com/fmpdsoresult" exclude-result-prefixes="fm">
  <xsl:output method="xml" indent="yes"/>

  <xsl:variable name="rename">
    <item from="reference" to="ref" />
    <item from="pu_date" to="date" />
   </xsl:variable>

  <xsl:template match="/*">
    <root>
      <xsl:apply-templates select="fm:ROW"/>
    </root>
  </xsl:template>


  <xsl:template match="fm:ROW">
    <transportbookings>
      <transportbooking>
        <xsl:apply-templates select="fm:reference | fm:pu_date" mode="rename" />
        </transportbooking>
    </transportbookings>
  </xsl:template>

  <xsl:template match="*" mode="rename">
    <xsl:element name="{document('')//xsl:variable[@name = 'rename']/item[@from = local-name(current())]/@to}">

           <xsl:attribute name="matchmode">0</xsl:attribute>

      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>`


This is what need to have:

    <root>
    <transportbookings>
    <transportbooking>
    <ref matchmode="0">260458</ref>
    <date>13-6-2023</date>   **! NO MATCHMODE**
    </transportbooking>
    </transportbookings>
    </root>

I tried an if statement but I could not get it to work. I think I'm making it to complex or ?

This is the RAW XML.

0 DTS.fmp12 DTS_XML 122 260458 <pu_date>13-6-2023</pu_date>


Solution

  • It seems you could do simply:

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fm="http://www.filemaker.com/fmpdsoresult" 
    exclude-result-prefixes="fm">
    <xsl:output method="xml" indent="yes"/>
    
    <xsl:template match="/FMPDSORESULT">
        <root>
            <xsl:apply-templates select="fm:ROW"/>
        </root>
    </xsl:template>
    
    <xsl:template match="fm:ROW">
        <transportbookings>
            <transportbooking>
                <xsl:apply-templates select="fm:reference | fm:pu_date"/>
            </transportbooking>
        </transportbookings>
    </xsl:template>
    
    <xsl:template match="fm:reference ">
        <ref matchmode="0">
            <xsl:value-of select="."/>
        </ref>
    </xsl:template>
    
    <xsl:template match="fm:pu_date" ">
        <date>
            <xsl:value-of select="."/>
        </date>
    </xsl:template>
    
    </xsl:stylesheet>
    

    But you gave us no input to test with. Note also that this creates transportbookings wrapper for each exported record. If - as it would seem from your example - you are exporting only one record, then this could be much shorter.

    I would also suggest you consider using the FMPXMLRESULT grammar for your export. This will protect your stylesheet against changes to field names.