简体   繁体   English

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

[英]How to include an XSL file into another XSL file

I need that 2.xsl file use function for changing date format (tag <Date> ) from file 1.xsl . 我需要2.xsl文件使用功能来从文件1.xsl更改日期格式(标记<Date> )。 I use instruction <xsl:include> but I don't know what I should to change. 我使用指令<xsl:include>但我不知道应该更改什么。

Here is my XML file: 这是我的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>

My 1.xsl file with function that changes date format: 我的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>

Here is my 2.xsl file (I need that this file use function from 1.xsl ): 这是我的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>

There are couple of issues with the XSLs created and rightly pointed out by @Martin Honnen in his comment related to 1.xsl . @Martin Honnen在与1.xsl相关的评论中创建并正确指出了XSL的一些1.xsl

The <Date> format conversion logic needs to be wrapped inside an <xsl:template> or <xsl:function> . <Date>格式转换逻辑需要包装在<xsl:template><xsl:function> Below is the updated 1.xsl that uses <xsl:template> . 以下是使用<xsl:template>的更新的1.xsl

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

In 2.xsl , you are including utils.xsl which I believe corresponds to 1.xsl . 2.xsl ,您包括utils.xsl ,我相信它对应于1.xsl In this XSL, you need to invoke the template or function that you have created in the included 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>

EDIT - Changes for using <xsl:function> 编辑 -使用<xsl:function>更改

XSLT 2.0 allows writing of own functions using <xsl:function> . XSLT 2.0允许使用<xsl:function>编写自己的函数。 Functions can be used for performing any computational logic and they return values based on the computations. 函数可用于执行任何计算逻辑,并且它们基于计算返回值。 Parameters can be passed to the function using <xsl:param> . 可以使用<xsl:param>将参数传递给函数。 Functions have to be associated to a namespace which is different than the XSLT namespace. 函数必须与不同于XSLT名称空间的名称空间相关联。

The 1.xsl will need to be modified to include a function which accepts a single parameter of type string and returns a value which is of type string . 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>

This function is associated with namespace xmlns:ex="http://canbeanything" . 此函数与名称空间xmlns:ex="http://canbeanything" Additionally since the datatypes are being used for the parameter and return value, you need to define namespace xmlns:xs="http://www.w3.org/2001/XMLSchema" also. 另外,由于数据类型用于参数和返回值,因此还需要定义名称空间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">

Changes in 2.xsl to invoke the function ex:DateConvertor . 2.xsl更改以调用函数ex:DateConvertor

Map the namespace necessary for the function. 映射功能所需的名称空间。

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ex="http://canbeanything">

Call Function and pass Date value as a parameter 调用函数并将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>

The required output can be achieved in a single XSL, but I am assuming that this is a learning phase and the above is probably an example of <xsl:include> . 所需的输出可以在单个XSL中实现,但是我假设这是一个学习阶段,上面可能是<xsl:include>的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM