【问题标题】:XSLT - Output in Text without XML TagsXSLT - 没有 XML 标签的文本输出
【发布时间】:2015-06-18 15:27:25
【问题描述】:

我已经使用 2 个 xslt 来生成预期的输出,但我得到的输出是子元素被合并并作为文本提供的。

所以,请告诉我

1) 在 XSLT 2 中可以添加什么来生成所需的输出。 2) 组合 2 个 xslt 并将其呈现为单个 xslt 的方法。 3) 编写 xslt 以生成所需输出的更好方法。

详情

输入

<?xml version="1.0" encoding="UTF-8"?>
<ns1:fnsetEngineReleased xmlns:ns1="http://from_sap.interfaces.oms"><ns1:strPO>DDDD</ns1:strPO><ns1:strEngine>ASAS</ns1:strEngine></ns1:fnsetEngineReleased>

预期输出

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:from="http://from_sap.interfaces.oms"> 
  <soap:Header/> 
  <soap:Body> 
     <from:fnsetEngineReleased> 
        <from:strPO>DDDD</from:strPO> 
        <from:strEngine>ASAS</from:strEngine> 
     </from:fnsetEngineReleased> 
  </soap:Body> 
</soap:Envelope>

得到的输出

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
               xmlns:from="http://from_sap.interfaces.oms">
   <soap:Header/>
   <soap:Body>
      <from:fnsetEngineReleased>DDDDASAS</from:fnsetEngineReleased>
   </soap:Body>
</soap:Envelope>

XSLT1 - 移除命名空间

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>
 <xsl:template match="*">
  <xsl:element name="{local-name()}">
   <xsl:apply-templates select="node()|@*"/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>

XSLT2 - 添加命名空间

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>
 <xsl:strip-space elements="*"/>
   <xsl:template match="/*">
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"  xmlns:from="http://from_sap.interfaces.oms" > 
<soap:Header></soap:Header>
<soap:Body>
            <xsl:element name="from:{local-name()}" namespace="http://from_sap.interfaces.oms">
            <xsl:apply-templates select="node()|@*" />
        </xsl:element>
         </soap:Body>
 </soap:Envelope>
   </xsl:template>
</xsl:stylesheet>

【问题讨论】:

    标签: xslt-2.0


    【解决方案1】:

    两个命名空间相同。您不需要任何花哨的东西,只需将输入复制到输出,您只需将其包装到 SOAP 信封中即可:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
                    version="1.0">
    
       <xsl:template match="/">
          <soap:Envelope> 
             <soap:Header/>
             <soap:Body>
                <xsl:copy-of name="*"/>
             </soap:Body>
          </soap:Envelope>
       </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2012-11-06
      • 1970-01-01
      • 2023-03-21
      • 2013-03-20
      • 2016-06-01
      相关资源
      最近更新 更多