2014-01-09 92 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