简体   繁体   English

使用XSLT的XML模板转换输出

[英]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. 我已经有了要转换的XML,并且为获得所需的输出而在XSLT中需要进行哪些更改,对此我感到困惑。

Where I'm getting hung up is on how to write the template to also include the data from ../Deliverables/Deliverable/Name. 我要挂断电话的地方是如何编写模板以同时包含来自../Deliverables/Deliverable/Name的数据。

Here is an excerpt of the XML 这是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 这是用于转换的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: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 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. 如果您尝试产生适合导入FileMaker的结果,则不是。


Edit: 编辑:

Yes, the transform needs to be suitable for FileMaker. 是的,转换需要适合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: 为了使输出符合FileMaker的FMPXMLRESULT模式,您需要添加描述字段的METADATA部分,将整个内容封装在FMPXMLRESULT标记中,然后将其放置在FileMaker的命名空间中:

<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; 将“ METADATA”部分中的字段名称更改为您的首选项; 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. 假设您使用的是FileMaker 11版或更高版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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