Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在xml消息中插入多个带前缀的名称空间_Xml_Xslt - Fatal编程技术网

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

在xml消息中插入多个带前缀的名称空间,xml,xslt,Xml,Xslt,我试图执行xslt转换,但没有得到我想要的结果。我需要在xml消息中插入几个带有前缀的名称空间 这是我得到的信息: <Operation> <SessionId>?</SessionId> <appUser> <number1>?</number1> <field2>?</

我试图执行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>

?        
?            
?            
?
?
?            

提前感谢您的帮助。

注意:这样的更改纯粹是装饰性的,与XML文档的语义无关

因为您使用的是XSLT2.0,所以在XML文档的最外层元素上使用
xsl:namespace
指令,并在那里声明
mp:
名称空间。这样做的方法是为
操作编写一个单独的模板

<xsl:template match="/*">
并将另一个命名空间节点添加到此元素:

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

<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>
<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:
也)上声明所有名称空间,这也是一种装饰性的措施。

非常感谢。这真的很有帮助和启发性。
<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>