xslt - 基于新值分组
问题描述
我正在做转换,我想通过新站点值添加值。仓库 150 和仓库 120 变成美国站点,而欧盟站点只有仓库 405。我该怎么做?
输入:
<Recordname>
<fld1>product1</fld1>
<fld2>warehouse150</fld2>
<fld3>13</fld3>
</Recordname>
<Recordname>
<fld1>product1</fld1>
<fld2>warehouse120</fld2>
<fld3>12</fld3>
</Recordname>
<Recordname>
<fld1>product1</fld1>
<fld2>warehouse405</fld2>
<fld3>22</fld3>
</Recordname>
<Recordname>
<fld1>product1</fld1>
<fld2>warehouse405</fld2>
<fld3>2</fld3>
</Recordname>
<Recordname>
<fld1>product2</fld1>
<fld2>warehouse150</fld2>
<fld3>7</fld3>
</Recordname>
<Recordname>
<fld1>product2</fld1>
<fld2>warehouse405</fld2>
<fld3>6</fld3>
</Recordname>
输出:
<Recordname>
<fld1>product1</fld1>
<site>US</site>
<fld3>25</fld3>
</Recordname>
<Recordname>
<fld1>product1</fld1>
<site>EU</site>
<fld3>24</fld3>
</Recordname>
<Recordname>
<fld1>product2</fld1>
<site>US</site>
<fld3>7</fld3>
</Recordname>
<Recordname>
<fld1>product2</fld1>
<site>EU</site>
<fld3>6</fld3>
</Recordname>
期待您的回答,因为我是样式表的新手。
解决方案
虽然可以一次完成,但更容易 - 更易读 - 分两次完成:首先重命名fld2
元素并使用您的站点名称填充它,然后按产品和站点对结果进行分组:
XSLT 2.0
<xsl:stylesheet version="2.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="*"/>
<!-- identity transform (for all modes) -->
<xsl:template match="@*|node()" mode="#all">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="#current"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/root">
<!-- first pass -->
<xsl:variable name="first-pass">
<xsl:apply-templates mode="first-pass"/>
</xsl:variable>
<!-- output -->
<xsl:copy>
<!-- group by product and site -->
<xsl:for-each-group select="$first-pass/Recordname" group-by="concat(fld1, '|', site)">
<xsl:copy>
<xsl:apply-templates mode="output"/>
</xsl:copy>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<!-- rename and repopulate fld2 -->
<xsl:template match="fld2" mode="first-pass">
<site>
<xsl:value-of select="if(.='warehouse405') then 'EU' else 'US'"/>
</site>
</xsl:template>
<!-- sum current group -->
<xsl:template match="fld3" mode="output">
<xsl:copy>
<xsl:value-of select="sum(current-group()/fld3)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
推荐阅读
- azure-devops - 如何设置我的 Az DevOps 管道以驻留在不同的分支中?
- ruby-on-rails - 如何使用引用直通表的范围编写`has_many:through`?
- mongodb - mongosql 未在 Red Hat 8 上启动:“加载共享库时出错:libssl.so.10:无法打开共享对象文件:没有这样的文件或目录”
- flutter - Flutter:如何使整行可点击
- gps - Onboard SDK 能否提供原始伪距和载波相位测量和星历数据?
- git - 尝试在 GitHub 上分叉任何 repo 会引发“你已经分叉了..再次分叉到另一个帐户”对话框
- angular-reactive-forms - 表单提交成功后出现验证错误
- c++ - 如何使 msvc 矢量化浮点加法?
- r - 如何从位于 URL 中的多个级别的 zip 文件在 R 中创建数据框?
- rust - 返回包含引用的结果时,不能一次多次借用“*self”作为可变变量