2012-08-09 71 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> 
相关问题