【问题标题】:Spring Integration xslt-transformer doesn't work with namespaceSpring Integration xslt-transformer 不适用于命名空间
【发布时间】:2012-08-30 19:35:58
【问题描述】:

我使用 Spring 集成:

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

它不起作用,但没有命名空间也能正常工作,如果你能提供帮助,不胜感激。

【问题讨论】:

    标签: spring spring-integration


    【解决方案1】:

    问题在于 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>
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多