简体   繁体   English

使用xsl将相同的命名空间添加到xml

[英]add same namespace to xml using xsl

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:t1="http://mynamespace/A" xmlns:top="http://mynamespace/B" 
    xmlns:max="http://mynamespace/C"><soapenv:Body>
  <t1:Creditcard>
     <top:AutoPayenroll>
        <top:CustomerId>
           <max:CustName>Taylor</max:CustName>
           <max:CustID>1234</max:CustID>
        </top:CustomerId>
     </top:AutoPayenroll>
  </t1:CreditCard></soapenv:Body></soapenv:Envelope>  

Need to change the CustID to encrypted one which I did. 需要将CustID更改为我所做的加密。 but dont know how to insert it 但不知道如何插入它

Used this XSL: 用过这个XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:t1="http://mynamespace/A" xmlns:top="http://mynamespace/B"
  xmlns:max="http://mynamespace/C" version="1.0">
  <xsl:output method="xml"/><xsl:template match="/"> 
<xsl:apply-templates/>   </xsl:template>  <xsl:template match="//*[local-name()='CustID']"> 
<xsl:variable name="cleartxt" select="./text()"/>  
<!--got this encrypted data from my internal code-->  
<xsl:variable name="encdata" select="'jksdguasidgeiruh'"/>  
<xsl:element name="//*[local-name()='Pswd']"> 
  <xsl:value-of select="$encdata"/> 
</xsl:element>   </xsl:template>   <xsl:template match="*"> 
<xsl:copy> <xsl:copy-of select="@*"/>  
  <xsl:apply-templates/> 
</xsl:copy>   </xsl:template> </xsl:stylesheet>    

Response should be as follows: 回应应如下:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:t1="http://mynamespace/A" xmlns:top="http://mynamespace/B" xmlns:max="http://mynamespace/C"><soapenv:Body>
  <t1:Creditcard>
     <top:AutoPayenroll>
        <top:CustomerId>
           <max:CustName>Taylor</max:CustName>
       <max:CustID>jksdguasidgeiruh</max:CustID>
        </top:CustomerId>
     </top:AutoPayenroll>
  </t1:CreditCard></soapenv:Body></soapenv:Envelope>      

Your example XSL and desired output are a little at odds, but whatever. 您的示例XSL和所需的输出有点不一致,但无论如何。

Have you tried something like: 你尝试过类似的东西:

<max:Pswd><xsl:value-of select="$encdata"/></max:Pswd>

In other words, you don't always need to use <xsl:element/> if just coding the desired output is sufficient. 换句话说,如果只编码所需的输出就足够了,你并不总是需要使用<xsl:element/>

As the max namespace is the same in the wanted result (isn't a different one), you only need to perform this short and simple transformation : 由于max命名空间在想要的结果中是相同的(不是不同的),您只需要执行这个简短的简单转换

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:max="http://mynamespace/C">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pEncrypted" select="'jksdguasidgeiruh'"/>

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

 <xsl:template match="max:CustID/text()">
  <xsl:value-of select="$pEncrypted"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document (corrected to be made well-formed!): 当对提供的XML文档应用此转换时(已更正为格式正确!):

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:t1="http://mynamespace/A"
xmlns:top="http://mynamespace/B"
xmlns:max="http://mynamespace/C">
    <soapenv:Body>
        <t1:Creditcard>
            <top:AutoPayenroll>
                <top:CustomerId>
                    <max:CustName>Taylor</max:CustName>
                    <max:CustID>1234</max:CustID>
                </top:CustomerId>
            </top:AutoPayenroll>
        </t1:Creditcard>
    </soapenv:Body>
</soapenv:Envelope>

the wanted, correct result is produced : 产生了想要的正确结果

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:t1="http://mynamespace/A"
 xmlns:top="http://mynamespace/B"
 xmlns:max="http://mynamespace/C">
   <soapenv:Body>
      <t1:Creditcard>
         <top:AutoPayenroll>
            <top:CustomerId>
               <max:CustName>Taylor</max:CustName>
               <max:CustID>jksdguasidgeiruh</max:CustID>
            </top:CustomerId>
         </top:AutoPayenroll>
      </t1:Creditcard>
   </soapenv:Body>
</soapenv:Envelope>

Explanation : 说明

Proper use and override of the identity rule/template . 正确使用和覆盖身份规则/模板

Update : 更新

The OP indicates in a comment that the namespaces can be different on each response and aren't known in advance. OP在注释中指出命名空间在每个响应上可以是不同的,并且事先不知道。

Here is the same solution, slightly modified to accomodate for this specification change; 这是相同的解决方案,稍作修改以适应此规范的变化;

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

 <xsl:param name="pEncrypted" select="'jksdguasidgeiruh'"/>

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

 <xsl:template match="*[local-name()='CustID']/text()">
  <xsl:value-of select="$pEncrypted"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied to the same XML document (above), the same correct, wanted result is produced : 当此转换应用于同一XML文档(上面)时,会生成相同的正确的想要结果

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:t1="http://mynamespace/A" xmlns:top="http://mynamespace/B" xmlns:max="http://mynamespace/C">
   <soapenv:Body>
      <t1:Creditcard>
         <top:AutoPayenroll>
            <top:CustomerId>
               <max:CustName>Taylor</max:CustName>
               <max:CustID>jksdguasidgeiruh</max:CustID>
            </top:CustomerId>
         </top:AutoPayenroll>
      </t1:Creditcard>
   </soapenv:Body>
</soapenv:Envelope>

Or more safely , (the OP stated in another comment that the namespace-uri s are the same and only the prefixes change): 或者更安全的是(OP在另一条评论中说明了名称空间-uri是相同的,只有前缀改变了):

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

 <xsl:param name="pEncrypted" select="'jksdguasidgeiruh'"/>

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

 <xsl:template match=
 "*[local-name()='CustID' and namespace-uri()='http://mynamespace/C']/text()">
  <xsl:value-of select="$pEncrypted"/>
 </xsl:template>
</xsl:stylesheet>

You can't use xpath in defining your element name like that. 您不能使用xpath来定义元素名称。 The below will replace CustId with Pswd in the max=http://mynamespace/C namespace, using the identity template to copy everything else: 下面将在max=http://mynamespace/C命名空间中用Pswd替换CustId ,使用身份模板复制其他所有内容:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:max="http://mynamespace/C"
                >
    <xsl:output method="xml" indent="yes"/>

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

    <!--Replace CustID with Pswd-->
    <xsl:template match="*[local-name()='CustID']">
        <xsl:variable name="cleartxt" select="./text()"/>
        <!--got this encrypted data from my internal code-->
        <xsl:variable name="encdata" select="'jksdguasidgeiruh'"/>
        <xsl:element name="max:Pswd">
            <xsl:value-of select="$encdata"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM