2012-08-09 78 views
2

我有這個有效載荷,我很有興趣知道我是如何將這個「TV:」命名空間前綴添加到所有節點和元素的。如何設置一個名稱空間前綴到一個有效載荷xsl

<TVInqResponse> 
     <TVInqRS> 
      <StatusCode>0</StatusCode> 
      <StatusDescription>Success</StatusDescription> 

This is what is expect to have as a result: 

<**tv**:TVInqResponse> 
     <**tv**:TVInqRS> 
      <**tv**:StatusCode>0</**tv**:StatusCode> 
      <**tv**:StatusDescription>Success</**tv**:StatusDescription> 

回答

1

該轉化

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:tv="some:tv"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

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

當此XML文檔上施加(基於畸形提供 「playload」 ...):

<TVInqResponse> 
<TVInqRS> 
    <StatusCode>0</StatusCode> 
    <StatusDescription>Success</StatusDescription> 
</TVInqRS> 
</TVInqResponse> 

產生想要的,正確的結果

<tv:TVInqResponse xmlns:tv="some:tv"> 
    <tv:TVInqRS> 
     <tv:StatusCode>0</tv:StatusCode> 
     <tv:StatusDescription>Success</tv:StatusDescription> 
    </tv:TVInqRS> 
</tv:TVInqResponse> 

如果你也想任何屬性名在同一個命名空間,更換模板匹配與屬性這個

<xsl:template match="@*"> 
    <xsl:attribute name="tv:{name()}" namespace="some:tv"> 
    <xsl:value-of select="."/> 
    </xsl:attribute> 
</xsl:template> 
相關問題