xml - 如何将 XSL 文件包含到另一个 XSL 文件中
问题描述
我需要该2.xsl
文件使用功能来从文件更改日期格式(标签<Date>
)1.xsl
。我使用指令<xsl:include>
,但我不知道我应该改变什么。
这是我的 XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<ClientList>
<Client>
<Name>Jan</Name>
<Surname>Kowalski</Surname>
<Date>2018-03-23</Date>
</Client>
<Client>
<Name>Piotr</Name>
<Surname>Nowak</Surname>
<Date>2018-04-25</Date>
</Client>
</ClientList>
我的1.xsl
文件具有更改日期格式的功能:
<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<Date>
<xsl:value-of select="concat(substring(Date, 9, 2), '-', substring(Date, 6, 2), '-', substring(Date, 1, 4))"/>
</Date>
</xsl:stylesheet>
这是我的2.xsl
文件(我需要这个文件使用来自 的函数1.xsl
):
<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:include href="1.xsl"/>
<xsl:template match="/">
<ClientList>
<xsl:for-each select="ClientList/Client">
<Client>
<NameSurname>
<xsl:value-of select="concat(Name, ' ' , Surname)"/>
</NameSurname>
</Client>
</xsl:for-each>
</ClientList>
</xsl:template>
</xsl:stylesheet>
解决方案
@Martin Honnen 在与1.xsl
.
<Date>
格式转换逻辑需要包含在or<xsl:template>
中<xsl:function>
。下面是1.xsl
使用的更新<xsl:template>
。
<xsl:template name="DateConvertor">
<Date>
<xsl:value-of select="concat(substring(Date, 9, 2), '-', substring(Date, 6, 2), '-', substring(Date, 1, 4))"/>
</Date>
</xsl:template>
在2.xsl
,你包括utils.xsl
我认为对应的1.xsl
。在此 XSL 中,您需要调用您在包含的 XSL 中创建的模板或函数。
<xsl:template match="/">
<ClientList>
<xsl:for-each select="ClientList/Client">
<Client>
<NameSurname>
<xsl:value-of select="concat(Name, ' ', Surname)" />
</NameSurname>
<xsl:call-template name="DateConvertor" /> <!-- call the template here -->
</Client>
</xsl:for-each>
</ClientList>
</xsl:template>
编辑- 使用更改<xsl:function>
XSLT 2.0 允许使用<xsl:function>
. 函数可用于执行任何计算逻辑,并根据计算返回值。可以使用 将参数传递给函数<xsl:param>
。函数必须与不同于 XSLT 命名空间的命名空间相关联。
1.xsl
需要修改 will 以包含一个函数,该函数接受一个类型的参数并string
返回一个类型的值string
。
<xsl:function name="ex:DateConvertor" as="xs:string">
<xsl:param name="InputDate" as="xs:string" />
<xsl:value-of select="concat(substring($InputDate, 9, 2), '-', substring($InputDate, 6, 2), '-', substring($InputDate, 1, 4))"/>
</xsl:function>
此函数与命名空间相关联xmlns:ex="http://canbeanything"
。此外,由于数据类型用于参数和返回值,因此您还需要定义命名空间xmlns:xs="http://www.w3.org/2001/XMLSchema"
。
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ex="http://canbeanything">
更改2.xsl
为调用函数ex:DateConvertor
。
映射函数所需的命名空间。
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ex="http://canbeanything">
调用函数并将Date
值作为参数传递
<xsl:template match="/">
<ClientList>
<xsl:for-each select="ClientList/Client">
<Client>
<NameSurname>
<xsl:value-of select="concat(Name, ' ', Surname)" />
</NameSurname>
<!-- Function Call -->
<!-- passing value of 'Date' as parameter -->
<Date>
<xsl:value-of select="ex:DateConvertor(Date)" />
</Date>
</Client>
</xsl:for-each>
</ClientList>
</xsl:template>
所需的输出可以在单个 XSL 中实现,但我假设这是一个学习阶段,上面可能是<xsl:include>
.
推荐阅读
- excel - Excel VBA宏:创建注释框并插入全尺寸图片
- ios - 使用 Carthage 添加静态框架会导致“无共享框架方案”
- java - 如何在创建 EAR 之前编译和构建 WAR
- laravel - 单击导航链接时更改页面语言
- sql - 处理跨时期的日期范围
- python - 在使用 Google Places api 时,我可以做些什么来解决这个错误“raise googlemaps.exceptions.Timeout()”?
- ios - 如何在 SwiftUI 中以横向模式预览设备?
- android - 无法使用导航组件清除所有片段的回栈
- sql - 如何解决数据库关系中的循环问题?
- linux - 如何在 Linux 中找到正在运行的进程的单个线程的 %RAM 和 %CPU 消耗?