2013-04-26 29 views
0

下面是我的XML和XSLT字符串返回屬性..XSLT轉換與指定的前綴

xml='<SampleXML> 
     <header> 
      <Id>123</Id> 
     </header> 
     <properties> 
      <property name="a1_name1" value="apple"/> 
      <property name="a1_name2" value="SALE"/> 
      <property name="a2_name3" value="20130425"/> 
     </properties> 

    </SampleXML> 


'; 

xslt ="<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\"> 
    <xsl:template match=\"SampleXML/header\"/> 
    <xsl:template match=\"SampleXML/properties\"> 
     <xsl:apply-templates select=\"property[position() = 1]\"> 
      <xsl:with-param name=\"mode\">name</xsl:with-param> 
     </xsl:apply-templates>;<xsl:apply-templates select=\"property[position() = 1]\"> 
      <xsl:with-param name=\"mode\">value</xsl:with-param> 
     </xsl:apply-templates>~</xsl:template> 

    <xsl:template match=\"property\"> 
    <xsl:param name=\"mode\" /> 
    <xsl:if test=\"$mode='name'\"> 
     <xsl:variable name=\"sub\"><xsl:apply-templates select=\"following-sibling::*[1]\"><xsl:with-param name=\"mode\" select=\"$mode\"/></xsl:apply-templates></xsl:variable> 
     <xsl:value-of select=\"@name\"/>|<xsl:value-of select=\"$sub\"/> 
    </xsl:if> 
    <xsl:if test=\"$mode='value'\"> 
     <xsl:variable name=\"sub\"><xsl:apply-templates select=\"following-sibling::*[1]\"><xsl:with-param name=\"mode\" select=\"$mode\"/></xsl:apply-templates></xsl:variable> 
     <xsl:value-of select=\"@value\"/>|<xsl:value-of select=\"$sub\"/> 
    </xsl:if> 
    </xsl:template>  
</xsl:stylesheet>"; 

這給我的

<?xml version="1.0"?> 
a1_name1|a1_name2|a2_name3|;apple|SALE|20130425|~ 

的輸出如何修改XSLT輸出看到只有那些帶有前綴'a1'的東西。像下面這樣的東西。嘗試使用模板匹配,但沒有運氣。

<?xml version="1.0"?> 
a1_name1|a1_name2;apple|SALE~ 

回答

0

嘗試是這樣的:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match="SampleXML/header"/> 
    <xsl:template match="SampleXML/properties"> 
     <xsl:for-each select="property[starts-with(@name,'a1')]"> 
      <xsl:value-of select="@name"/> 
      <xsl:if test ="position() != last()"> 
       <xsl:text>|</xsl:text> 
      </xsl:if> 
     </xsl:for-each> 
     <xsl:text>;</xsl:text> 
     <xsl:for-each select="property[starts-with(@name,'a1')]"> 
      <xsl:value-of select="@value"/> 
      <xsl:if test ="position() != last()"> 
       <xsl:text>|</xsl:text> 
      </xsl:if> 
     </xsl:for-each> 
     <xsl:text>~</xsl:text> 
</xsl:template> 
</xsl:stylesheet> 

這將產生:

a1_name1|a1_name2;apple|SALE~ 
0

在回答你的問題,如果你想排除財產元件,其屬性首先「A1」只需添加下面的模板忽略然後

<xsl:template match="property[not(starts-with(@name, 'a1_'))]" /> 

注意如何模板立即關閉,所以是空的。換句話說,它什麼都不做。當它匹配其name屬性不以'a1_'開頭的屬性元素時,將不會輸出任何內容。

XSLT處理器將首先匹配最具體的模板,因此該優先級將忽略這些元素。

但是,值得注意的是,您可能可以簡化當前的XSLT。您目前正在傳遞模式屬性,以確定要輸出哪個屬性。這樣做會爲另一種方法如下:

<xsl:template match="property"> 
    <xsl:param name="mode"/> 
    <xsl:value-of select="@*[name() = $mode]"/> 
    <xsl:text>|</xsl:text> 
</xsl:template> 

注意有沒有需要調用XSL:申請模板一個元素的時間,只需選擇他們都在一個單獨的語句。

<xsl:apply-templates select="property"> 
    <xsl:with-param name="mode" select="'name'"/> 
</xsl:apply-templates> 

他們將順序來選擇,所以它不會是一個問題。試試這個XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match="SampleXML/header"/> 

    <xsl:template match="SampleXML/properties"> 
     <xsl:apply-templates select="property"> 
     <xsl:with-param name="mode" select="'name'"/> 
     </xsl:apply-templates> 
     <xsl:text>;</xsl:text> 
     <xsl:apply-templates select="property"> 
     <xsl:with-param name="mode" select="'value'"/> 
     </xsl:apply-templates> 
     <xsl:text>~</xsl:text> 
    </xsl:template> 

    <xsl:template match="property[not(starts-with(@name, 'a1_'))]"/> 

    <xsl:template match="property"> 
     <xsl:param name="mode"/> 
     <xsl:value-of select="@*[name() = $mode]"/> 
     <xsl:text>|</xsl:text> 
    </xsl:template> 
</xsl:stylesheet> 

當適用於您的XML,下面是輸出

a1_name1|a1_name2|;apple|SALE|~ 
+0

IM困惑。爲什麼它輸出那些以a1 w開頭的如果我嘗試其他方式,它會給我a2_name3 |; 20130425 |〜..爲什麼會發生這種情況? – 2013-04-26 19:16:21

+0

我修改了問題來解釋但。這是因爲模板''匹配**名稱以'a1_'開頭的屬性**元素。但模板是空的。模板沒有代碼,所以當它匹配一個元素時它什麼也不做,所以元素不會得到輸出。 – 2013-04-27 08:26:34