2012-09-25 83 views
1
<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> 

需要将CustID更改为我所做的加密的一个。但不知道如何将它插入使用xsl将相同的名称空间添加到xml

使用此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>  

反应应该是如下:

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

我工作在你的形式打一些。你能编辑你的问题并修复它的其余部分吗?这对我们来说会更容易阅读和理解。 – LarsH

+0

此外,如果您要输出带有加密文本内容的“”,并且您的“响应应该是”,但您的XSLT表示您要输出带有该内容的“”。你要哪个? – LarsH

+0

请执行@LarsH写的内容。我们希望网站上的问题能够正确格式化,以便使用。 – hakre

回答

1

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

当这个变换所提供的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> 

的希望,正确的结果产生

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

说明:的identity rule/template

正确使用和覆盖。

更新

的OP在注释表示该命名空间可以在每个响应不同和预先是未知的。

以下是同样的解决方案,稍作修改以适应此规范更改;

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

当这种转换应用于同一个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> 

或者更安全的,(在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> 
+0

是同意的,但每次命名空间前缀有时会更改ns1,有时ns2也是如此。所以我想使用那个请求中传入的内容,而不用在xsl – mnvbrtn

+0

@ user1698404中进行硬编码,从你的评论中不清楚什么改变了 - 请解释一下。 –

+0

命名空间t1,top,max每次都在变化。我的意思是命名空间网址是相同的,但前缀不同 – mnvbrtn

2

你举的例子XSL和期望的输出都有点别扭,但不管。

你有没有尝试过这样的:

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

换句话说,你并不总是需要使用<xsl:element/>如果仅仅编码所需的输出就足够了。

+0

是的我知道我们可以使用max命名空间,但每次命名空间前缀有时会更改ns1,有时ns2也是如此。所以我想使用它在那个请求中传入的内容,而不用在xsl – mnvbrtn

+0

中进行硬编码,我不知道为什么要保留任意分配的名称空间前缀。尽管如此,这些额外的要求让Dimitre能够展示一些不错的技术。 –

0

你不能用xpath来定义你的元素名称。下面将在max=http://mynamespace/C命名空间替换CustIdPswd,使用身份模板复制一切:

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