【问题标题】:How to set a namespace prefix to a payload xsl如何将命名空间前缀设置为有效负载 xsl
【发布时间】:2012-08-09 13:42:31
【问题描述】:

我有这个有效载荷,我很想知道如何将这个“TV:”命名空间前缀添加到其中的所有节点和元素。

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

【问题讨论】:

    标签: xslt xpath namespaces prefix


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多