xslt - XSLT 标记插入
问题描述
我有一个多级 XML 块,我的要求是为每个 Employee 块注入一个新的 Scenario 标签。新场景标签中的值可以根据事件标签中的值更改
从
<Extract>
<Header>
<Date1>01/01/2020</Date1>
</Header>
<Employee>
<Status>
<Event>Event_1</Event>
</Status>
</Employee>
<Employee>
<Status>
<Event>Event_2</Event>
</Status>
</Employee>
</Extract>
至
<Extract>
<Header>
<Date1>01/01/2020</Date1>
</Header>
<Employee>
<Status>
<Event>Event_1</Event>
</Status>
<Scenario>A</Scenario>
</Employee>
<Employee>
<Status>
<Event>Event_2</Event>
</Status>
<Scenario>B</Scenario>
</Employee>
</Extract>
我一直在玩这个,我能够成功插入标签,但是在添加标签之前,我希望有一个选择语句来确定标签中所需的输出。如下
<xsl:param name="to-insert">
<xsl:choose>
<xsl:when test="Employee/Status/Event = 'Event_1' ">
<Scenairo>A</Scenairo>
</xsl:when>
<xsl:when test="Employee/Status/Event = 'Event_2' ">
<Scenairo>B</Scenairo>
</xsl:when>
<xsl:otherwise>
<Scenairo><xsl:value-of select="'no scenario mapped'"/></Scenairo>
</xsl:otherwise>
</xsl:choose>
</xsl:param>
<!-- Copy all sections of XML -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="Employee">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:copy-of select="$to-insert"/>
</xsl:copy>
</xsl:template>
解决方案
总是落入 xsl:否则
不,但它只评估第一个Employee
(在 XSLT 1.0 中)或所有员工一起(在 XSLT 2.0 中)。
如果您可以拥有多个,则需要从上下文中Employee
计算 的值- 例如:Scenario
Employee
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"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Employee">
<xsl:copy>
<xsl:apply-templates/>
<Scenario>
<xsl:choose>
<xsl:when test="Status/Event = 'Event_1'">A</xsl:when>
<xsl:when test="Status/Event = 'Event_2'">B</xsl:when>
<xsl:otherwise>no scenario mapped</xsl:otherwise>
</xsl:choose>
</Scenario>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
推荐阅读
- javascript - 触发后如何修改css动画?
- python - 情绪分析逻辑回归的错误输入形状
- python - 将值返回到 tkinter 接口
- ios - 按钮上有两种颜色(快速)
- python-3.x - 从每行数据框的字符串列表中创建一个新的数据框
- c++ - 如何使用 doxygen 在 markdown 中自动创建指向 C++ 类的链接?
- php - Laravel 7 Vue Dropzone - 无法显示存储中的图像。文件名没有上传到 phpmyadmin
- sapui5 - 私人 UI5 开发
- java - 2D Array number grid with for and if
- python-3.x - 覆盖 __getitem__