在xml消息中插入几个带前缀的名称空间

cps工程师

我正在尝试执行xslt转换,但没有得到想要的结果。我需要将几个带有前缀的名称空间插入到xml消息中。

这是我收到的消息:

  <Operation>        
     <SessionId>?</SessionId>        
     <appUser>            
        <number1>?</number1>            
        <field2>?</field2>            
        <Properties>
           <!--Zero or more repetitions:-->
           <KeyValue>
              <Key>?</Key>
              <Value>?</Value>
           </KeyValue>
        </Properties>            
        <value3>?</value3>            
     </appUser>
  </Operation>

这是我所做的转换:

<xsl:stylesheet version="2.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="*">
  <xsl:element name="ser:{name()}" namespace="http://www.url.com/Services">
    <xsl:copy-of select="namespace::*"/>
    <xsl:apply-templates select="node()|@*"/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="*[ancestor::appUser]">
  <xsl:element name="mp:{name()}" namespace="http://schemas.data.org/myPath">
    <xsl:copy-of select="namespace::*"/>
    <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
 </xsl:template>

  <xsl:template match="*[ancestor-or-self::KeyValue ]">
    <xsl:element name="arr:{name()}" namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <xsl:copy-of select="namespace::*" />
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

我得到以下内容:

<ser:Operation xmlns:ser="http://www.url.com/Services">        
         <ser:SessionId>?</ser:SessionId>        
         <ser:appUser>            
            <mp:number1 xmlns:mp="http://schemas.data.org/myPath">?</mp:number1>            
            <mp:field2 xmlns:mp="http://schemas.data.org/myPath">?</mp:field2>            
            <mp:Properties xmlns:mp="http://schemas.data.org/myPath">
               <!--Zero or more repetitions:-->
               <arr:KeyValue xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
                  <arr:Key>?</arr:Key>
                  <arr:Value>?</arr:Value>
               </arr:KeyValue>
            </mp:Properties>            
            <mp:value3 xmlns:mp="http://schemas.data.org/myPath">?</mp:value3>            
         </ser:appUser>
      </ser:Operation>

但是我不喜欢在每个元素中看到名称空间声明xmlns:mp =“ http://schemas.data.org/myPath”。

因此,我可以修改我的xslt以获得类似以下内容的东西吗?

<ser:Operation xmlns:ser="http://www.url.com/Services" xmlns:mp="http://schemas.data.org/myPath">        
         <ser:SessionId>?</ser:SessionId>        
         <ser:appUser>            
            <mp:number1 >?</mp:number1>            
            <mp:field2>?</mp:field2>            
            <mp:Properties>
               <!--Zero or more repetitions:-->
               <arr:KeyValue xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
                  <arr:Key>?</arr:Key>
                  <arr:Value>?</arr:Value>
               </arr:KeyValue>
            </mp:Properties>            
            <mp:value3>?</mp:value3>            
         </ser:appUser>
      </ser:Operation>

预先感谢您的帮助。

马蒂亚斯·穆勒(MathiasMüller)

注意:这样的更改纯粹是表面上的-与XML文档的语义无关。

由于您使用的是XSLT 2.0,因此请使用xsl:namespaceXML文档最外层元素上指令,并在其中声明mp:名称空间。为此的方法是为以下代码编写单独的模板Operation

<xsl:template match="/*">

构造一个以开头的新元素 ser:

<xsl:element name="ser:{name()}">

并将另一个名称空间节点添加到此元素:

<xsl:namespace name="mp" select="'http://schemas.data.org/myPath'"/>

然后,XSLT处理器将避免序列化输出中的冗余名称空间声明。

另外,如果在xsl:stylesheet元素中声明所有名称空间,而不是在代码中的其他位置,则样式表更易读

样式表

<xsl:stylesheet version="2.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ser="http://www.url.com/Services"
  xmlns:mp="http://schemas.data.org/myPath"
  xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays">

 <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="/*">
     <xsl:element name="ser:{name()}">
     <xsl:namespace name="mp" select="'http://schemas.data.org/myPath'"/>
         <xsl:apply-templates select="node()|@*"/>
     </xsl:element>
 </xsl:template>

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

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

  <xsl:template match="*[ancestor-or-self::KeyValue ]">
    <xsl:element name="arr:{name()}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

XML输出

<ser:Operation xmlns:mp="http://schemas.data.org/myPath"
               xmlns:ser="http://www.url.com/Services">
   <ser:SessionId>?</ser:SessionId>
   <ser:appUser>
      <mp:number1>?</mp:number1>
      <mp:field2>?</mp:field2>
      <mp:Properties><!--Zero or more repetitions:-->
         <arr:KeyValue xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
            <arr:Key>?</arr:Key>
            <arr:Value>?</arr:Value>
         </arr:KeyValue>
      </mp:Properties>
      <mp:value3>?</mp:value3>
   </ser:appUser>
</ser:Operation>

当然,您也可以在最外面的元素上声明所有名称空间arr:,这同样是一种装饰性的措施。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从嵌入式资源中删除带前缀的名称空间

XML中是否可以有多个名称空间前缀?

使用Unmarshal在Go中获取XML名称空间前缀

在Java DOM中创建以名称空间为前缀的XML节点

带xpath的python etree和带前缀的名称空间

XML名称空间前缀冲突的XPath行为

在Eclipse的XHTML + XML文件中搜索并替换XML名称空间前缀

如何在JAVA中编码默认名称空间和带有单个XML元素前缀的名称空间

用多个带前缀的命名空间构造XML信封

如何获取样式表中定义的名称空间前缀,而不是从输入XML中获取

在具有名称空间前缀的元素内创建不带名称空间前缀的XML元素

使用根元素语法中的名称空间前缀解析XML-Java

在PowerShell中创建从父节点继承名称空间前缀的XML元素

使用XQuery检查是否在整个文件中重新定义了XML文档的名称空间前缀

抑制 XML 文件中的命名空间前缀

如何从 xml 中删除前缀/命名空间

LXML名称空间前缀

XML:从名称空间生成带有前缀的标签

如何通过前缀获取xml文件的名称空间(常规)

更改使用JAXWS生成的默认XML名称空间前缀

xml名称空间前缀问题随处可见

XML:子节点是否继承父节点的名称空间前缀?

如何用内联xmlns声明替换XML名称空间前缀?

XML错误:前缀未绑定到名称空间,KML文件

如何使用XSLT删除特定xml元素的名称空间前缀?

python 2.7 XML lxml名称空间前缀属性问题

如何使用Xstream从xml删除名称空间前缀?

将XML名称空间前缀替换为完整的名称空间URI值?

从外壳程序向XML文档的默认名称空间的标签添加名称空间前缀。