XSLT - 删除前缀命名空间特定节点XML
问题描述:
这是我的XML与SOAP标题和正文:XSLT - 删除前缀命名空间特定节点XML
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<RequestResponse xmlns="http://tempuri.org/">
<a:RequestResult xmlns:a="http://schemas.datacontract.org/2004/07/MockupTesting"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Message>Message text testing.</a:Message>
<a:Response>false</a:Response>
</a:RequestResult>
</RequestResponse>
</s:Body>
</s:Envelope>
我需要从RequestResult
节点只删除前缀。 从这个
<a:RequestResult xmlns:a="http://schemas.datacontract.org/2004/07/MockupTesting"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
要:
<RequestResult xmlns:a="http://schemas.datacontract.org/2004/07/MockupTesting"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
这是XSLT
配置文件我有2版本中使用:
<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="/">
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<xsl:apply-templates />
</s:Body>
</s:Envelope>
</xsl:template>
<xsl:template match="RequestResult |RequestResult//*">
<xsl:element name="a:{name()}"
namespace="http://schemas.datacontract.org/2004/07/Testing">
<xsl:namespace name="a"
select="'http://schemas.datacontract.org/2004/07/MockupTesting'" />
<xsl:namespace name="i"
select="'http://www.w3.org/2001/XMLSchema-instance'" />
<!-- <xsl:copy-of select="namespace::*" /> -->
<xsl:apply-templates select="node()|@*" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
我要补充或修改,以去除该节点上的前缀?
答
无法从节点“删除前缀”。前缀是节点名称的一部分。要删除前缀,你必须创建使用其他名称,可能还有新的节点 - 在您的例子 - 在另一个命名空间:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="http://schemas.datacontract.org/2004/07/MockupTesting">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="a:RequestResult">
<xsl:element name="RequestResult" namespace="http://tempuri.org/">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
+0
但是,如果我添加该命名空间,它将无法工作 – gtx911
+0
@ gtx911请停止在谜语中谈话。上面的转换会产生你所要求的**精确**结果。如果你想要别的东西,编辑你的问题并澄清。同样的事情适用于您的其他问题:http://*.com/a/40589393/3016153 –
你不能“删除”命名空间前缀,在绝大多数情况下,你不需要做那样的事情。你能解释一下为什么你认为这是必要的吗? – Tomalak
由于我使用的WSDL,它需要@Tomalak – gtx911
对不起,这不是一个令人满意的解释。我问:“你为什么需要它?”你回答说:“因为我需要它。”我认为你需要给出一个比这更好的理由。 – Tomalak