2017-09-05 111 views
0

我們可以添加自定義的標籤或功能XSLT.I再解釋一下我在演示中添加一個標籤include-html。可我們在XSLT添加任何邏輯,當我們在我的樣式表中找到include-html它匹配與標籤值模板(與我們做在apply-template)並獲取它的值並顯示在輸出中。如何在xslt中添加自定義標籤?

這裏是我的代碼。 http://xsltransform.net/6pS1zDt/1

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" /> 

    <xsl:template match="/"> 
     <hmtl> 
     <head> 
      <title>New Version!</title> 
     </head> 
     <include-html>a</include-html> 
     </hmtl> 
    </xsl:template> 

    <xsl:template match="a"> 
    <xsl:variable name="ab" select="'ss'"/> 
    <p><xsl:value-of select="$ab"/></p> 
</xsl:template> 
</xsl:transform> 

在我的例子我寫include-html value.No的a值與之相匹配的模板,並返回**<p>ss</p>**

<include-html>a</include-html> 

預計輸出

**<p>ss</p>** 
+0

您需要重新開始HT tps://www.w3schools.com/xml/xsl_intro.asp –

+0

@MatthewWhited我知道這些事情 – naveen

回答

0

你可以做到這一點某種元樣式表(le T公司將其命名爲test.xslt):

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" /> 

    <!-- identity template --> 
    <xsl:template match="node()|@*"> 
     <xsl:copy> 
     <xsl:apply-templates select="node()|@*" /> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="/whatever"> 
     <hmtl> 
     <head> 
      <title>New Version!</title> 
     </head> 
     <include-html>a</include-html> 
     </hmtl> 
    </xsl:template> 

    <xsl:template match="include-html[text()='a']"> 
    <xsl:variable name="ab" select="'ss'"/> 
    <p><xsl:value-of select="$ab"/></p> 
    </xsl:template> 

</xsl:transform> 

如果傳遞給你的XSLT處理器XSLT 和像

xsl.sh test.xslt test.xslt 

XML輸入輸出

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="xml" 
       omit-xml-declaration="yes" 
       encoding="UTF-8" 
       indent="yes"/> 

    <!-- identity template --> 
    <xsl:template match="node()|@*"> 
     <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="/whatever">  <!-- must NOT match --> 
     <hmtl> 
     <head> 
      <title>New Version!</title> 
     </head> 
     <p>ss</p>      <!-- REPLACED! --> 
     </hmtl> 
    </xsl:template> 

    <xsl:template match="include-html[text()='a']"> 
     <xsl:variable name="ab" select="'ss'"/> 
     <p> 
     <xsl:value-of select="$ab"/> 
     </p> 
    </xsl:template> 

</xsl:transform> 

這是接近你想要什麼,我想...

相關問題