在 Cdata 中使用 XSLT 1.0 删除 xml 声明(<?xml 版本= "1.0" 编码= "UTF-8" ?>)



我正在从类似的SharePoint应用程序获得响应

输入

 <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <SharepointResponse xmlns="http://test.com.services.generic">
            <Sharepoint_Response>&lt;?xml version="1.0" encoding="UTF-8"?>
&lt;CopyIntoItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
   &lt;CopyIntoItemsResult>0&lt;/CopyIntoItemsResult>
   &lt;Results>
      &lt;CopyResult ErrorCode="Success" DestinationUrl="http://archivelink.dev.test.com/"/>
   &lt;/Results>
&lt;/CopyIntoItemsResponse></Sharepoint_Response>
        </SharepointResponse>
    </Body>
</Envelope>

我正在使用此代码

代码

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes" encoding="utf-8"/>
    <!--Identity template simply copies content forward -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
     <xsl:template match="*">
        <xsl:copy>
            <xsl:apply-templates select="*" />
            <xsl:value-of select="text()" disable-output-escaping="yes"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

输出:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <SharepointResponse xmlns="http://test.com.services.generic">
            <Sharepoint_Response><?xml version="1.0" encoding="UTF-8"?>
<CopyIntoItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
   <CopyIntoItemsResult>0</CopyIntoItemsResult>
   <Results>
      <CopyResult ErrorCode="Success" DestinationUrl="http://archivelink.dev.test.com/enterprise/"/>
   </Results>
</CopyIntoItemsResponse></Sharepoint_Response>
            </SharepointResponse>
        </Body>
    </Envelope>

我不知道如何在<Sharepoint_Response> 之后删除此<?xml version="1.0" encoding="UTF-8"?>

预期输出:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <SharepointResponse xmlns="http://test.com.services.generic">
            <Sharepoint_Response>
<CopyIntoItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
   <CopyIntoItemsResult>0</CopyIntoItemsResult>
   <Results>
      <CopyResult ErrorCode="Success" DestinationUrl="http://archivelink.dev.test.com/enterprise/"/>
   </Results>
</CopyIntoItemsResponse></Sharepoint_Response>
            </SharepointResponse>
        </Body>
    </Envelope>

你可以试试这样的东西:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes" indent="yes" encoding="utf-8" />
  <!--Identity template simply copies content forward -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>
  <xsl:template match="*">
    <xsl:copy>
      <xsl:apply-templates select="*" />
      <xsl:value-of select="substring-after(text(),'?&gt;')" disable-output-escaping="yes" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

其思想是使用substring-after来获取XML声明的?>部分结束后的所有内容。使用该样式表得到了以下输出:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
  <Body>
    <SharepointResponse xmlns="http://test.com.services.generic">
      <Sharepoint_Response>
<CopyIntoItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
   <CopyIntoItemsResult>0</CopyIntoItemsResult>
   <Results>
      <CopyResult ErrorCode="Success" DestinationUrl="http://archivelink.dev.test.com/"/>
   </Results>
</CopyIntoItemsResponse></Sharepoint_Response></SharepointResponse></Body></Envelope>

最新更新