如何添加XSL命名空间自定义标记

时间:2013-03-29 12:01:16

标签: xslt

我有源XML

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body>
<PublishANZINCIDENTESB xmlns="http://www.ibm.com/maximo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" creationDateTime="2013-02-25T23:25:35+00:00" transLanguage="EN" baseLanguage="EN" messageID="1361834735434709840" maximoVersion="7 1 20110105-1024 V7118-37" event="1"><ANZINCIDENTESBSet><INCIDENT action="Replace"></INCIDENT></ANZINCIDENTESBSet></PublishANZINCIDENTESB></soapenv:Body></soapenv:Envelope>

我想通过在标记中添加名称空间和标题来更改为目标XML。输出XML是

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:s600="http://LIB_ISM_WPS/S600_PublishANZINCIDENT_Service_INT" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Header/><soapenv:Body>
<s600:PublishANZINCIDENTESBOperation><PublishANZINCIDENTESB xmlns="http://www.ibm.com/maximo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" creationDateTime="2013-02-25T23:25:35+00:00" transLanguage="EN" baseLanguage="EN" messageID="1361834735434709840" maximoVersion="7 1 20110105-1024 V7118-37" event="1"><ANZINCIDENTESBSet><INCIDENT action="Replace"></INCIDENT></ANZINCIDENTESBSet></PublishANZINCIDENTESB></s600:PublishANZINCIDENTESBOperation></soapenv:Body></soapenv:Envelope>

我正在使用XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:s600="http://LIB_ISM_WPS/S600_PublishANZINCIDENT_Service_INT" >
 <s600:PublishANZINCIDENTESBOperation>
 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>
</s600:PublishANZINCIDENTESBOperation>
 <xsl:template match="*">
  <xsl:element name="s600:{name()}" namespace="http://LIB_ISM_WPS/S600_PublishANZINCIDENT_Service_INT">
   <xsl:copy-of select="namespace::*"/>
       <xsl:apply-templates select="node()|@*"/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>

任何人都可以尽快帮助我,因为它正在解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

从您的输入和输出示例看起来您只想在s600:PublishANZINCIDENTESBOperation的内容周围包含soapenv:Body元素,否则保持内容不变。您可以使用

实现此目的
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:s600="http://LIB_ISM_WPS/S600_PublishANZINCIDENT_Service_INT"
 xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">

  <xsl:template match="@*|node()">
    <xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
  </xsl:template>

  <xsl:template match="soapenv:Body">
    <xsl:copy>
      <s600:PublishANZINCIDENTESBOperation>
        <xsl:apply-templates select="@*|node()" />
      </s600:PublishANZINCIDENTESBOperation>
    </xsl:copy>
  </xsl:template>

  <!-- If the namespace declaration absolutely _must_ be on the Envelope 
       you could uncomment this extra template -->
  <!--
  <xsl:template match="soapenv:Envelope">
    <soapenv:Envelope>
      <xsl:apply-templates select="@*|node()" />
    </soapenv:Envelope>
  </xsl:template>
  -->
</xsl:stylesheet>

这将生成您正在寻找的输出,但xmlns:s600声明将位于s600:PublishANZINCIDENTESBOperation元素而不是soapenv:Envelope上,但这应该无关紧要下游组件提供了他们使用适当的XML解析器来处理数据而不是将其视为文本。如果在信封上包含声明非常重要,请取消注释第三个模板。