簡體   English   中英

xslt套用范本並進行比對

[英]xslt apply template and match

這不是我的代碼。 但是它與我想要的類似,這就是為什么我在這里使用它的原因。 我從這里得到的

<xsl:template match="/">
<xsl:apply-templates select="event/details">
<xsl:with-param name="title" select="event/title"/> <!-- pass param "title" to matching templates -->
</xsl:apply-templates>
</xsl:template>

<xsl:template match="details">
<xsl:param name="title"/> <!-- this template takes parameter "title" -->
Title: <xsl:value-of select="$title"/><br/>
Timestamp: <xsl:value-of select="java:DateUtil.getDate(number(timestamp))"/><br/>
Description: <xsl:value-of select="description"/><br/>
</xsl:template>

我的問題是我可以在上面帶有參數的匹配模板“詳細信息”和沒有參數的模板中進行匹配嗎? 對不起,我的英語..讓我知道,如果我沒有道理,我會盡力改寫。 謝謝你。

編輯:這是我想要的。

template 1 - with parameter:

<xsl:template match="details">
<xsl:param name="title"/> <!-- this template takes parameter "title" -->
Title: <xsl:value-of select="$title"/><br/>
Timestamp: <xsl:value-of select="java:DateUtil.getDate(number(timestamp))"/><br/>
Description: <xsl:value-of select="description"/><br/>
</xsl:template>

template 2 - without parameter:

<xsl:template match="/">
<xsl:apply-templates select="event/details"/>
</xsl:template>

<xsl:template match="details">
Timestamp: <xsl:value-of select="java:DateUtil.getDate(number(timestamp))"/><br/>
Description: <xsl:value-of select="description"/><br/>
</xsl:template>

I.具有相同匹配模式的兩個模板是可恢復的錯誤-在最佳情況下,將僅選擇其中一個模板來執行

在您的特定示例中,您可以僅將模板與參數一起使用,並對其代碼稍作修改,以便在參數沒有值(空字符串)時不寫入任何標題。

這是一個小示范如何做到這一點

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates select="event/details"/>
        ===========
        <xsl:apply-templates select="event/details">
          <xsl:with-param name="title" select="'Title Provided'"/>
        </xsl:apply-templates>

    </xsl:template>

    <xsl:template match="details">
        <xsl:param name="title"/>

    <xsl:value-of select=
    "concat(substring('Title: ',
                      1 + 7*not(string-length($title) > 0)
                      ),
            $title)
    "/>
        <br/> Timestamp: 
        <xsl:value-of select="'someTimeStamp'"/>
        <br/> Description: 
        <xsl:value-of select="description"/>
        <br/>
    </xsl:template>

</xsl:stylesheet>

在此轉換中,模板匹配details被調用兩次-第一次不帶參數,第二次帶$title參數。 在這兩種情況下,模板都會生成所需的輸出

<br/> Timestamp: 
        someTimeStamp<br/> Description: 
        <br/>
        ===========
        Title: Title Provided<br/> Timestamp: 
        someTimeStamp<br/> Description: 
        <br/>

二。 xsl:function XSLT 2.0中的xsl:function

使用xsl:function可以實現所需的xsl:function -用XSLT編寫的函數-此功能僅在XSLT 2.0(及更高版本)中可用。 完全有可能編寫同一個函數的不同重載,而我們有很多這樣的例子。

您的兩個模板的本質不同是說“如果提供了標題,則顯示它”。 為此,可以將參數默認設置為空序列(在XSLT 2.0中,將xs :: param添加select="()" ,在1.0中,將select="/.."添加到),然后將條件邏輯添加到形式為“如果$ title存在,則顯示它”的模板。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM