You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为FileMaker提取的XML中特定元素添加XSL属性

解决FileMaker XML导出中仅为添加matchmode属性的问题

尝试从FileMaker数据库生成XML文件,已实现基础XSL结构,但当前生成的XML中和都带有matchmode="0"属性,需求是仅保留该属性,不需要,求正确的XSL配置。

当前生成的XML

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

编写的XSL

<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>

期望的XML结果

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

原始XML

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

修改后的XSL配置

只需要在添加matchmode属性的代码块外增加条件判断,仅当原元素为fm:reference(对应输出的<ref>)时才添加该属性:

<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}">
      <!-- 仅为原reference元素添加matchmode属性 -->
      <xsl:if test="local-name() = 'reference'">
        <xsl:attribute name="matchmode">0</xsl:attribute>
      </xsl:if>
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

修改说明

原模板中无条件给所有经过rename模式处理的元素添加matchmode属性,现在通过xsl:if判断当前处理元素的本地名称是否为reference(对应输出的<ref>),只有符合条件时才添加属性,<date>对应的原元素是pu_date,不会触发该条件,因此不会带有matchmode属性。


内容的提问来源于stack exchange,提问作者Hans

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 14:15:01