我有以下XML。我必須在肥皂包裹下包括肥皂身體。XSLT - 複製多個名稱空間
<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。 我在XSL的元素名稱中嘗試了許多選項命名空間選項以及命名空間。 任何人都可以幫我解決這個問題。
由於提前, 納傑馬
好問題+1這是瞭解最基本和最強大的XSLT設計模式的好機會 - 重寫身份規則,使用這種模式可以輕鬆解決問題,並且以簡化的標準方式解決諸如此類的許多問題,它要求基本上「按原樣」複製XML文檔,僅引入小的本地化修改。 –