如何将XSL文件包含到另一个XSL文件中

时间:2018-05-03 16:37:20

标签: xml xslt xslt-2.0

我需要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>

1 个答案:

答案 0 :(得分:2)

@Martin Honnen在与1.xsl相关的评论中创建并正确指出了XSL的几个问题。

<Date>格式转换逻辑需要包含在<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以包含一个函数,该函数接受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>的一个例子。