我有以下XML(許多數據是假的)XSLT轉換改變命名空間
<?xml version="1.0" encoding="UTF-8"?>
<msg xmlns="http://someaddress.com/m1" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://someaddress/somexsd.xsd">
<header>
<nodeA>aaaaaaaaaaa</nodeA>
<nodeB>bbbbbbbb</nodeB>
</header>
<payload>
<calcnode xmlns="http://someaddress/nodeC" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://someaddress/somexsd.xsd">
<somefield>field</somefield>
</calcnode>
<ds:Signature>
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
<SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
<Reference URI="#kjbn34jkb5j-3k45j-k3jb534jkb534k5">
<DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<DigestValue>+se0dfgft9gh8hjuji7ji65ko4ko3ko2</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>sekfrhsdkjfhsdkjfhksd</SignatureValue>
<KeyInfo>
<X509Data>
<X509Certificate>sdjkfhsdkfhskdf</X509Certificate>
</X509Data>
</KeyInfo>
</ds:Signature>
</payload>
</msg>
所有我想要做的就是改變msg
元素的名稱(以newmsg
)和默認命名空間http://someaddress.com/m2
。其他一切應該保持原樣。我已經能夠獲得最接近的是這個XSLT
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msg="http://someaddress.com/m1" >
<!-- the identity template -->
<xsl:template match="@* | node()">
<xsl:copy copy-namespaces="no">
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<!-- replace namespace of elements in old namespace -->
<xsl:template match="msg:msg">
<xsl:element name="newmsg" namespace="http://someaddress.com/m2">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
我使用Xalan的測試,這是輸出XML運行以上
<?xml version="1.0" encoding="UTF-8"?>
<newmsg xmlns="http://someaddress.com/m2" xsi:schemaLocation="http://someaddress/somexsd.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<header xmlns="http://someaddress.com/m1" xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<nodeA>aaaaaaaaaaa</nodeA>
<nodeB>bbbbbbbb</nodeB>
</header>
<payload xmlns="http://someaddress.com/m1" xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<calcnode xmlns="http://someaddress/nodeC" xsi:schemaLocation="http://someaddress/somexsd.xsd">
<somefield>field</somefield>
</calcnode>
<ds:Signature>
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
<SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
<Reference URI="#kjbn34jkb5j-3k45j-k3jb534jkb534k5">
<DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<DigestValue>+se0dfgft9gh8hjuji7ji65ko4ko3ko2</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>sekfrhsdkjfhsdkjfhksd</SignatureValue>
<KeyInfo>
<X509Data>
<X509Certificate>sdjkfhsdkfhskdf</X509Certificate>
</X509Data>
</KeyInfo>
</ds:Signature>
</payload>
</newmsg>
當我有這個2個主要問題:
- 命名空間
ds
只是從輸出中消失,我不知道爲什麼。 - 第二個問題是,它從
calcnode
中刪除了xsi
名稱空間,並且假定calcnode
是用於計算(並驗證)簽名的節點(它位於該節的下面),這種更改將使簽名的驗證不可能到我的理解。
我已經試過了XSLT的幾個不同迭代沒有太多的結果。任何人都可以看到這種情況嗎?
Xalan是XSLT 1.0處理器,但您的樣式表是XSLT 2.0。改用撒克遜9.4。 – mzjn