gpt4 book ai didi

xml - XSLT - 需要复制多个命名空间

转载 作者:行者123 更新时间:2023-12-01 23:30:14 24 4
gpt4 key购买 nike

我有以下 XML。我必须将肥皂体放在肥皂信封下。

<headers>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:def="http://lmn" xmlns:abc="http://xyz"><soapenv:Header><abc:Security> <abc:UsernameToken>
<abc:Username>abc</abc:Username>
<abc:Password>pwd</abc:Password>
</abc:UsernameToken>
</abc:Security>
</soapenv:Header>
</soapenv:Envelope>
</headers>

我的输出应该是这样的

     <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:def="http://lmn" xmlns:abc="http://xyz">
<soapenv:Header>
<abc:Security>
<abc:UsernameToken>
<abc:Username>abc</abc:Username>
<abc:Password>pwd</abc:Password>
</abc:UsernameToken>
</abc:Security>
</soapenv:Header>
<soapenv:Body><a><b></b><c></c></a></soapenv:Body>
</soapenv:Envelope>

以下是我的 XSL。

 <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:soap="http://www.w3.org/2003/05/soap-envelope" exclude-result-prefixes="xd soap soapenv">
<xsl:template match="/">
<xsl:variable name="envNode"><xsl:copy-of select="name(headers/*)"/></xsl:variable>
<xsl:choose>
<xsl:when test="contains($envNode,'soapenv')">
<xsl:element name="{name(headers/*)}">
<xsl:copy-of select="headers/soapenv:Envelope/*"/>
<xsl:element name="soapenv:Body">
<a><b></b><c></c></a>
</xsl:element>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{name(soapOptions/headers/*)}">
<xsl:copy-of select="soapOptions/headers/soap:Envelope/*"/>
<xsl:element name="soap:Body">
<a><b></b><c></c></a>
</xsl:element>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

我得到以下输出。

  <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Header xmlns:def="http://lmn" xmlns:abc="http://xyz"">
<abc:Security>
<abc:UsernameToken>
<abc:Username>abc</abc:Username>
<abc:Password>pwd</abc:Password>
</abc:UsernameToken>
</abc:Security>
</soapenv:Header>
<soapenv:Body><a><b/><c/></a></soapenv:Body>
</soapenv:Envelope>

我想将soapenv:Envelope 节点中的所有命名空间复制到同一节点。但它被复制到soapenv:Header。 我尝试了许多选项 namespace 选项以及 XSL 中元素名称中的 namespace 。 谁能帮我解决这个问题。

提前致谢,纳吉玛

最佳答案

在元素声明下添加:

 <xsl:copy-of select="headers/*/namespace::*"/>

这会将所需的命名空间复制到正确的元素中,从而防止 XSLT 处理器在其他位置声明它们。

<小时/>

XSLT 验证:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">

<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
<xsl:element name="{name(headers/*)}">
<xsl:copy-of select="headers/*/namespace::*"/>
<xsl:copy-of select="headers/soapenv:Envelope/*"/>
<xsl:element name="soapenv:Body">
<a><b></b><c></c></a>
</xsl:element>
</xsl:element>

</xsl:template>

</xsl:stylesheet>

产生:

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:def="http://lmn" xmlns:abc="http://xyz">
<soapenv:Header>
<abc:Security>
<abc:UsernameToken>
<abc:Username>abc</abc:Username>
<abc:Password>pwd</abc:Password>
</abc:UsernameToken>
</abc:Security>
</soapenv:Header>
<soapenv:Body>
<a>
<b/>
<c/>
</a>
</soapenv:Body>
</soapenv:Envelope>
<小时/>

但是,如果您可以更改样式表,我会使用更简单的内容,例如:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">

<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="headers">
<xsl:apply-templates select="node()"/>
</xsl:template>

<xsl:template match="soapenv:Header">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<soapenv:Body>
<a><b></b><c></c></a>
</soapenv:Body>
</xsl:template>

</xsl:stylesheet>

关于xml - XSLT - 需要复制多个命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7453499/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com