2014-01-09 139 views
0

我在XSLT首發,我堅持這一點:XSL模板匹配精確節點

abc.xml

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
    <ns2:procesResponse xmlns:ns2="http://schemas.com/2014/generic"> 
     <a> 
     <b> 
      <c> 
      <d>test</d> 
      <e>someValue</e> 
      </c> 
     </b> 
     <b> 
      <c> 
      <d>test 2</d> 
      <e>someValue</e> 
      </c> 
     </b> 
     <b> 
      <c> 
      <d>test</d> 
      <e>someValue</e> 
      </c> 
     </b> 
     </a> 
    </ns2:procesResponse> 
    </soap:Body> 
</soap:Envelope> 

我做了什麼至今:

doit.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match="/"> 
    <something> 
     <stillSomething> 
     <Author>administrator</Author> 
     <Version>V1_4</Version> 
     <Date>09012014</Date> 
     </stillSomething> 
     <values> 
     <xsl:apply-templates select="Envelope/Body/procesResponse/a/b/c" /> 
     </values> 
    </something> 
    </xsl:template> 
    <xsl:template match="Envelope/Body/procesResponse/a/b/c"> 
     <result>succeeded</result> 
    </xsl:template> 
</xsl:stylesheet> 

x後的結果sltproc執行:

爲result.xml

<something> 
    <stillSomething> 
    <Author>administrator</Author> 
    <Version>V1_4</Version> 
    <Date>09012014</Date> 
    </stillSomething> 
    <values/> 
</something> 

但我想獲得這個:

<something> 
    <stillSomething> 
    <Author>administrator</Author> 
    <Version>V1_4</Version> 
    <Date>09012014</Date> 
    </stillSomething> 
    <values> 
    <result>succeeded</result> 
    <result>succeeded</result> 
    <result>succeeded</result> 
    </values> 
</something> 

成功應該有三倍三個節點被發現。

我知道,問題是在這兩條線:

<xsl:apply-templates select="Envelope/Body/procesResponse/a/b/c" /> 

<xsl:template match="Envelope/Body/procesResponse/a/b/c"> 

謝謝您的幫助!

+0

你輸入XML使用未宣佈任何地方的'ns2'前綴。你忽略了嗎?是否還有其他名稱空間聲明是你省略的? – JLRishe

+0

我手寫這個例子,忘了添加它。現在做了。沒有其他聲明。這會在沒有警告或錯誤的情況下執行。 – Matjaz

回答

1

這裏最大的問題是未能正確使用名稱空間。一旦他們在XSLT聲明,並在XPath使用,那麼這將產生預期的結果:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
       xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
       xmlns:ns2="http://schemas.com/2014/generic" 
       exclude-result-prefixes="soap ns2"> 
    <xsl:template match="/"> 
    <something> 
     <stillSomething> 
     <Author>administrator</Author> 
     <Version>V1_4</Version> 
     <Date>09012014</Date> 
     </stillSomething> 
     <values> 
     <xsl:apply-templates 
      select="soap:Envelope/soap:Body/ns2:procesResponse/a/b/c" /> 
     </values> 
    </something> 
    </xsl:template> 
    <xsl:template match="c"> 
    <result>succeeded</result> 
    </xsl:template> 
</xsl:stylesheet> 

結果:

<something> 
    <stillSomething> 
    <Author>administrator</Author> 
    <Version>V1_4</Version> 
    <Date>09012014</Date> 
    </stillSomething> 
    <values> 
    <result>succeeded</result> 
    <result>succeeded</result> 
    <result>succeeded</result> 
    </values> 
</something> 
+0

這是問題所在,我沒有聲明命名空間。謝謝 :) – Matjaz