首页 > 解决方案 > 如何将 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>

标签: xmlxsltxslt-2.0

解决方案


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


推荐阅读