2011-08-01 64 views
8

我想調用我自己的xsl模板,只要我找到xml元素的匹配,該元素的屬性值以「標題」開頭。如何在Xslt中進行此查詢。如何根據屬性值選擇xml元素從xslt中的「Heading」開始?

例如:

<w:p> 
    <w:pPr> 
    <w:pStyle w:val="Heading2"/> 
    </w:pPr> 
</w:p> 

<w:p> 
    <w:pPr> 
    <w:pStyle w:val="Heading1"/> 
    </w:pPr> 
</w:p> 

<w:p> 
    <w:pPr> 
    <w:pStyle w:val="Heading2"/> 
    </w:pPr> 
</w:p> 

<w:p> 
    <w:pPr> 
    <w:pStyle w:val="ListParagraph"/> 
    </w:pPr> 
</w:p> 

<w:p> 
    <w:pPr> 
    <w:pStyle w:val="commentText"/> 
    </w:pPr> 
</w:p> 

所以,我想讓查詢寬:PSTYLE - >女:VAL開始時只 「標題」。

請幫我走出這個問題...

回答

13

你可以做到這一點通過利用XPath字符串函數的開始與

<xsl:template match="w:pStyle[starts-with(@w:val, 'Heading')]"> 

這只是匹配所有寬:PSTYLE其中w:val屬性以標題開頭。然後你可以把你自己的代碼放在這個模板中。

這裏是你將如何使用它在XSLT身份的例子變換

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:w="http://mynamespace.com"> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="w:pStyle[starts-with(@w:val, 'Heading')]"> 
     <!-- Your code here --> 
    </xsl:template> 

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

以上XSLT,除非你是添加你自己的代碼,它說,將去掉所有mathcing 寬:PSTYLE來自XML的元素。