无法更新XML文档的命名空间 [英] Unable to Update the Namespace of XML Document

查看:72
本文介绍了无法更新XML文档的命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成XML输出的程序. 具有默认名称空间的程序如下所示:

I have a program that generates XML Output. The program with default namespace looks like this:

<n0:eCPR xmlns:n0="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd" 
         xmlns:prx="urn:sap.com:proxy:DV4:/1SAI/TAS1F59A417878D36573F1D:700:2013/05/24">
  <n0:contractorInfo>
   <n0:contractorName>test_user</n0:contractorName>
   <n0:contractorAddress>
    <n0:street></n0:street>
    <n0:city></n0:city>
    <n0:state/>
    <n0:zip/>
   </n0:contractorAddress>
 </n0:contractorInfo>
</n0:eCPR>

如果我删除默认名称空间,则输出如下所示

If i remove the default namespace, the output looks like this

<eCPR>
  <contractorInfo>
    <contractorName>test_user</contractorName>
    <contractorAddress>
     <street></street>
     <city></city>
     <state/>
     <zip/>
   </contractorAddress>
   <insuranceNum></insuranceNum>
   <contractorEmail></contractorEmail>
 </contractorInfo>
</eCPR>

我希望它显示为

<CPR:eCPR xmlns:CPR="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd">
 <CPR:contractorInfo>
  <CPR:contractorName>test_user</CPR:contractorName>
  <CPR:contractorAddress>
   <CPR:street>999 Carrier Rd</CPR:street>
   <CPR:city>Oakland</CPR:city>
   <CPR:state>CA</CPR:state>
   <CPR:zip>94612</CPR:zip>
  </CPR:contractorAddress>
 </CPR:contractorInfo>
</CPR:eCPR>

我用来删除默认名称空间的代码是:

The code i used to remove the default namespace was:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

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

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

请帮忙.

谢谢

推荐答案

鉴于您的输入没有属性(或注释或处理说明),我看不出您为什么不能简单地这样做:

Given that your input has no attributes (or comments or processing instructions), I see no reason why you couldn't do simply:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:CPR="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

</xsl:stylesheet>

这篇关于无法更新XML文档的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆