简体   繁体   中英

Template Transform Output of XML with XSLT

I've got this XML that I'm trying to transform and I'm stumped on what needs to change in my XSLT to achieve the output needed.

Where I'm getting hung up is on how to write the template to also include the data from ../Deliverables/Deliverable/Name.

Here is an excerpt of the XML

 <?xml version="1.0" encoding="UTF-8" ?> <Projects> <Project> <ProjectNum>P-999999</ProjectNum> <Name> <![CDATA[EXAMPLE PROJECT XYZ]]> </Name> <Tasks> <Task> <Deliverables> <Deliverable> <Name> <![CDATA[X-12345]]> </Name> <Product> <ProductCode> <![CDATA[123456]]> <Package> <![CDATA[CASE]]> <UUID> <![CDATA[037XXXXXX21]]> </UUID> </Package> </ProductCode> <ProductCode> <![CDATA[222333]]> <Package> <![CDATA[ITEM]]> <UUID> <![CDATA[000XXXXXX52723]]> </UUID> </Package> </ProductCode> </Product> </Deliverable> </Deliverables> </Task> <Task> <Deliverables> <Deliverable> <Name> <![CDATA[Y-12345]]> </Name> <Product> <ProductCode> <![CDATA[78910]]> <Package> <![CDATA[BOX]]> <UUID> <![CDATA[123XXXXXX45]]> </UUID> </Package> </ProductCode> <ProductCode> <![CDATA[4444555]]> <Package> <![CDATA[PALLET]]> <UUID> <![CDATA[678XXXXXX91011]]> </UUID> </Package> </ProductCode> </Product> </Deliverable> </Deliverables> </Task> </Tasks> </Project> </Projects> 

Here is the XSLT for the transform

 <?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:strip-space elements="*" /> <xsl:template match="/"> <FMPXMLRESULT> <RESULTSET> <xsl:apply-templates select="node() | @*" /> </RESULTSET> </FMPXMLRESULT> </xsl:template> <xsl:template match="ProductCode"> <ROW> <xsl:call-template name="fpc" /> </ROW> </xsl:template> <xsl:template name="fpc"> <COL> <DATA> <xsl:value-of select="normalize-space(./text())" /> </DATA> </COL> <xsl:for-each select="child::*"> <xsl:call-template name="fpc" /> </xsl:for-each> </xsl:template> <xsl:template match="Tasks/Task"> <COL> <DATA> <xsl:value-of select="./Deliverables/Deliverable/Name" /> </DATA> </COL> <xsl:for-each select="."> <xsl:call-template name="fpc" /> </xsl:for-each> </xsl:template> </xsl:stylesheet> 

Here is what I need the output to look like

 <?xml version="1.0" encoding="UTF-8" ?> <RESULTSET> <ROW> <COL> <DATA>X-12345</DATA> </COL> <COL> <DATA>123456</DATA> </COL> <COL> <DATA>CASE</DATA> </COL> <COL> <DATA>037XXXXXX21</DATA> </COL> </ROW> <ROW> <COL> <DATA>X-12345</DATA> </COL> <COL> <DATA>222333</DATA> </COL> <COL> <DATA>ITEM</DATA> </COL> <COL> <DATA>000XXXXXX52723</DATA> </COL> </ROW> <ROW> <COL> <DATA>Y-12345</DATA> </COL> <COL> <DATA>78910</DATA> </COL> <COL> <DATA>BOX</DATA> </COL> <COL> <DATA>123XXXXXX45</DATA> </COL> </ROW> <ROW> <COL> <DATA>Y-12345</DATA> </COL> <COL> <DATA>4444555</DATA> </COL> <COL> <DATA>PALLET</DATA> </COL> <COL> <DATA>678XXXXXX91011</DATA> </COL> </ROW> </RESULTSET> 

Any help you could offer would be much appreciated! Thank you.

The requested output can be obtained by:

XSLT 1.0

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

<xsl:template match="/Projects">
    <RESULTSET>
        <xsl:for-each select="Project/Tasks/Task/Deliverables/Deliverable/Product/ProductCode">
            <ROW>
                <COL>
                    <DATA><xsl:value-of select="normalize-space(ancestor::Deliverable/Name)"/></DATA>
                </COL>
                <COL>
                    <DATA><xsl:value-of select="normalize-space(text())"/></DATA>
                </COL>
                <COL>
                    <DATA><xsl:value-of select="normalize-space(Package/text())"/></DATA>
                </COL>
                <COL>
                    <DATA><xsl:value-of select="normalize-space(Package/UUID)"/></DATA>
                </COL>
            </ROW>
        </xsl:for-each>
    </RESULTSET>
</xsl:template>

</xsl:stylesheet>

Note:

If you are trying to produce a result suitable for importing into FileMaker, this is not it.


Edit:

Yes, the transform needs to be suitable for FileMaker. What would I do differently in the stylesheet to do so?

To make the output conform to FileMaker's FMPXMLRESULT schema, you need to add a METADATA section describing the fields, enclose the whole thing in FMPXMLRESULT tags, and place it in FileMaker's namespace:

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

<xsl:template match="/Projects">
    <FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
        <METADATA>
            <FIELD NAME="Name"/>
            <FIELD NAME="ProductCode"/>
            <FIELD NAME="Package"/>
            <FIELD NAME="UUID"/>
        </METADATA>
        <RESULTSET>
            <xsl:for-each select="Project/Tasks/Task/Deliverables/Deliverable/Product/ProductCode">
                <ROW>
                    <COL>
                        <DATA><xsl:value-of select="normalize-space(ancestor::Deliverable/Name)"/></DATA>
                    </COL>
                    <COL>
                        <DATA><xsl:value-of select="normalize-space(text())"/></DATA>
                    </COL>
                    <COL>
                        <DATA><xsl:value-of select="normalize-space(Package/text())"/></DATA>
                    </COL>
                    <COL>
                        <DATA><xsl:value-of select="normalize-space(Package/UUID)"/></DATA>
                    </COL>
                </ROW>
            </xsl:for-each>
        </RESULTSET>
    </FMPXMLRESULT>
</xsl:template>

</xsl:stylesheet>

Notes:

  1. Change the field names in the METADATA section to your preference; if you make them the same as the field names in your target solution, you will be able to use the "matching names" options when setting up the import map.
  2. This assumes you are using FileMaker version 11 or higher.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM