Spring Integration xslt-transformer不能与命名空间一起使用

时间:2012-08-30 19:35:59

标签: spring spring-integration

我使用Spring Integration:

<int-xml:xslt-transformer input-channel="partnerTransformation" 
output-channel="serviceChannel" 
xsl-resource="classpath:/META-INF/vendorTransformerXml.xsl"/>

输入XML消息:

<ns1:persons  xmlns:ns1="http://com.test.xslt/test"  xmlns:ns3="http://universal.consumerrequest.schema.model.tci.ca">
<ns3:person>
    <ns3:name>John</ns3:name>
    <ns3:family>Smith</ns3:family>
</ns3:person>
<ns3:person>
    <ns3:name>Raza</ns3:name>
    <ns3:family>Abbas</ns3:family>
</ns3:person>

和XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="http://com.test.xslt/test" xmlns:ns3="http://universal.consumerrequest.schema.model.tci.ca">


<xsl:output method="text"/>
<xsl:template match="/">
<xsl:for-each select="ns1:persons/ns3:person">


<xsl:value-of select="ns3:family"/>
<xsl:value-of select="ns3:name"/>
</xsl:for-each> 
</xsl:template>
</xsl:stylesheet>

它不起作用,但它没有名字就能很好地运作,如果你能提供帮助,那就表示赞赏。

1 个答案:

答案 0 :(得分:0)

问题是Java XML解析器默认处理命名空间的方式。你可以做一些事情:

  • 在将输入XML提供给XSLT转换器之前,将输入XML转换为String。这在我的测试中运行良好
  • 如果您自己构建文档,例如

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    Document doc = dbf.newDocumentBuilder().parse(...);
    
  • 如果您不必担心命名空间,可能最简单的方法是更改​​您的XSLT:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
         xmlns:ns1="http://com.test.xslt/test"
     xmlns:ns3="http://universal.consumerrequest.schema.model.tci.ca">
    
    
     <xsl:output method="text" />
      <xsl:template match="/">
        <xsl:for-each select="//*[local-name()='person']">
           <xsl:value-of select="*[local-name()='family']" />
           <xsl:value-of select="*[local-name()='name']" />
        </xsl:for-each>
      </xsl:template>
    </xsl:stylesheet>
    
相关问题