2015-04-20 14 views
0

我有這樣的XML:如何反轉義的元素,並刪除namespace屬性

<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,UNESCAPE,然後取出namespace屬性。

我已經使用這個成功實現了第一部分:

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

但是,我無法刪除namespace屬性,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> 

and

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

沒有成功。我如何刪除名稱空間屬性?

回答

2

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

謝謝。是否有可能將兩者結合起來?對不起,我對XSLT比較陌生。 – Simon

+0

@Simon您使用的是什麼版本的XSLT和處理器? –

+0

使用.Net 3.5的XSLT 1.0 – Simon

相關問題