使用xsl向xml添加相同的命名空间 泰勒 1234
需要将CustID更改为我所做的加密的CustID。但我不知道如何插入它 使用此XSL:使用xsl向xml添加相同的命名空间 泰勒 1234,xml,xslt,soap,Xml,Xslt,Soap,需要将CustID更改为我所做的加密的CustID。但我不知道如何插入它 使用此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
<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>
答复如下:
<?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>
泰勒
jksdguasidgeiruh
您的示例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>jksdguasidgeiruh</max:CustID>
</top:CustomerId>
</top:AutoPayenroll>
</t1:CreditCard></soapenv:Body></soapenv:Envelope>
换句话说,如果只对所需的输出进行编码就足够了,那么并不总是需要使用
。在这样定义元素名时不能使用xpath。以下内容将用max中的Pswd
替换CustId
=http://mynamespace/C
namespace,使用标识模板复制所有其他内容:
<max:Pswd><xsl:value-of select="$encdata"/></max:Pswd>
由于所需结果中的
max
名称空间相同(不是不同的名称空间),因此只需执行此简短的转换即可:
<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>
<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>
<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文档上应用此转换时(更正为格式正确!):
泰勒
1234
生成所需的正确结果:
<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>
<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>
<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>
泰勒
jksdguasidgeiruh
说明:
<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>
<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>
<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>
正确使用和覆盖
更新:
<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>
<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>
<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>
OP在注释中指出,每个响应上的名称空间可能不同,并且事先不知道
这是相同的解决方案,稍作修改以适应此规格变化
<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>
将此转换应用于相同的XML文档(如上)时,将生成相同的正确的所需结果:
<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>
<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>
<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>
泰勒
jksdguasidgeiruh
或更安全地说,(OP在另一条注释中指出名称空间uri相同,只有前缀发生变化):
我对您的格式进行了一些修改。你能编辑你的问题并解决剩下的问题吗?这将使我们更容易阅读和理解。此外,您的“响应应该是”表示您希望使用加密文本内容输出
,但XSLT表示您希望使用该内容输出
。你想要什么?请照@LarsH写的做。我们希望在网站上的问题是正确的格式,所以他们是有用的。是的,我知道我们可以使用最大的名称空间,但每次名称空间前缀改变有时ns1,有时ns2这样。因此,我想使用在该请求中传递的内容,而不使用XSL硬编码。我不知道为什么要保留任意分配的名称空间前缀。尽管如此,这些额外的要求还是让Dimitre展示了一些不错的技术。是的,我同意,但是每次名称空间前缀都在改变,有时是ns1,有时是ns2。因此,我想使用在该请求中传递的内容,而无需硬编码xsl@user1698404,从您的评论中不清楚什么正在更改为什么--请解释一下。名称空间t1,top,max每次都在更改。我的意思是名称空间URL是相同的,但前缀是相同的different@user1698404,再次更新--以涵盖命名空间URI未更改的注释。