如何取消转义元素并删除命名空间属性

How to unescape an element and remove the namespace attribute

我有这个XML:

<DiaryEvent name="CreateAppointment">
  <appointmentId>e69cbf2e-3de7-e411-9fbf-b161700bfb88</appointmentId>
  <originalStartDateTime>2015/04/20 11:15</originalStartDateTime>
  <originalEndDateTime>2015/04/20 11:30</originalEndDateTime>
  <originalAssignee>DOMAIN\user</originalAssignee>
  <initialData>&lt;Task id="b1520763-1369-482e-9133-1e40e5b476d0" userName="DOMAIN\user" createdAt="2015/04/20 10:10:25" formTypeId="00000000-0000-0000-0000-000000000000" formTypeName="Client Visit" isScheduled="true" minimumFormVersion="0" xmlns="http://mycompany.com/Schemas/TaskXmlSchema/1.0/"&gt;
  &lt;DataItems&gt;
    &lt;DataItem name="QLCl_Client No" type="int"&gt;123&lt;/DataItem&gt;
  &lt;/DataItems&gt;
&lt;/Task&gt;</initialData>
</DiaryEvent>

我需要从 <initialData> 元素中提取 XML,取消转义,然后删除命名空间属性。

我已经成功地实现了第一部分:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" version="1.0" indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="/">
        <xsl:copy>
            <xsl:value-of select="//initialData" disable-output-escaping="yes"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

但是,我无法从输出中删除命名空间属性 xmlns="http://mycompany.com/Schemas/TaskXmlSchema/1.0/"

<Task id="b1520763-1369-482e-9133-1e40e5b476d0" userName="DOMIAN\user" 
      createdAt="2015/04/20 10:10:25" 
      formTypeId="00000000-0000-0000-0000-000000000000" 
      formTypeName="Client Visit" 
      isScheduled="true" 
      minimumFormVersion="0" 
      xmlns="http://mycompany.com/Schemas/TaskXmlSchema/1.0/">
  <DataItems>
    <DataItem name="QLCl_Client No" type="int">123</DataItem>
  </DataItems>
</Task>

我试过各种组合:

<xsl:template match="@xmlns" />

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

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:t="http://mycompany.com/Schemas/TaskXmlSchema/1.0/" 
                exclude-result-prefixes="t" >

没有成功。如何删除命名空间属性?

xmlns:不是属性节点,是命名空间节点。因此,@xmlns 永远不会匹配。

如果我的理解正确,具有两个单独的转换对您来说不是问题,请按照此标准方法删除输入文档中元素和属性上存在的所有命名空间。

样式表需要您能够生成的未转义的中间文档作为输入。

XSLT 样式表

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output indent="yes" method="xml" encoding="UTF-8"/>

    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="@*">
        <xsl:attribute name="{local-name()}">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>

    <xsl:template match="comment()|text()|processing-instruction()">
        <xsl:copy/>
    </xsl:template>

</xsl:stylesheet>

XML输出

<?xml version="1.0" encoding="UTF-8"?>
<Task id="b1520763-1369-482e-9133-1e40e5b476d0"
      userName="DOMIAN\user"
      createdAt="2015/04/20 10:10:25"
      formTypeId="00000000-0000-0000-0000-000000000000"
      formTypeName="Client Visit"
      isScheduled="true"
      minimumFormVersion="0">
  <DataItems>
      <DataItem name="QLCl_Client No" type="int">123</DataItem>
  </DataItems>
</Task>