2012-08-30 23 views
0

工作,我用Spring集成:Spring集成XSLT變壓器不與命名空間

<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> 

它不起作用,但它窩很好,沒有名字,很感謝你能幫忙。

回答

0

問題是Java XML解析器默認處理命名空間的方式。您可以執行以下幾項操作:

  • 將輸入XML轉換爲字符串,然後將其提供給XSLT轉換器。這在我的測試中工作正常
  • 如果您正在自己構建文檔,例如

    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>