[英]How to include an XSL file into another XSL file
我需要2.xsl
文件使用功能來從文件1.xsl
更改日期格式(標記<Date>
)。 我使用指令<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
相關的評論中創建並正確指出了XSL的一些1.xsl
。
<Date>
格式轉換邏輯需要包裝在<xsl:template>
或<xsl:function>
。 以下是使用<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>
在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>
的示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.