3

私は次のXMLを持っています(データの多くはダミーです)

<?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つの主な問題があります。

  1. 名前ds空間が出力から消えるだけで、理由はわかりません。
  2. xsi2番目の問題は、から名前空間を削除し、calcnodecalcnode署名の計算(および検証)に使用されるノード(そのセクションの下にあります)であるとすると、そのような変更により、署名の検証が理解できなくなることです。

私はxsltのいくつかの異なる反復を試しましたが、多くの結果は得られませんでした。誰かがその状況に光を当てることができますか?

4

3 に答える 3

1

この変換:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:oldDef="http://someaddress.com/m1" exclude-result-prefixes="oldDef">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="*">
  <xsl:element name="{name()}" namespace="{namespace-uri()}">
   <xsl:copy-of select="namespace::*[name()]|@*"/>
   <xsl:apply-templates select="node()"/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="*[namespace-uri()='http://someaddress.com/m1']">
  <xsl:element name="{name()}" namespace="http://someaddress.com/m2">
   <xsl:copy-of select="namespace::*[name()]"/>
   <xsl:copy-of select="@*"/>
   <xsl:apply-templates select="node()"/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="oldDef:msg[true()]">
  <newmsg xmlns="http://someaddress.com/m2">
   <xsl:copy-of select="namespace::*[name()]|@*"/>
   <xsl:apply-templates select="node()"/>
  </newmsg>
 </xsl:template>
</xsl:stylesheet>

提供された XML ドキュメントに適用した場合:

<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>

必要な正しい結果が生成されます。

<newmsg xmlns="http://someaddress.com/m2" 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" 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>
于 2012-09-15T04:32:45.643 に答える
0

XSLTの要素でds名前空間を定義してみてください<xsl:stylesheet。ただし、ds名前空間は失われませんが、newmsgのすべての子要素に配布されるだけです。同様に、xsi名前空間はルート要素(newmsg)に適用されるため、calcnodeにも適用されます。

一部の名前空間宣言が移動したにもかかわらず、出力は有効であるように見えます。

いくつかの洞察を提供する可能性のある名前空間の制御に関する情報を含む記事を書きました:XSLTIDテンプレートを使用したXHTMLの変換

于 2012-09-14T15:41:29.640 に答える
0

やりたいことは、msg 要素の名前を (newmsg に) 変更し、デフォルトの名前空間をhttp://someaddress.com/m2に変更することだけです。それ以外はそのままにしておく必要があります。

スタイルシート:

<xsl:stylesheet version="2.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msg="http://someaddress.com/m1">

  <xsl:output omit-xml-declaration="yes"/>

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

  <!-- Process all elements (except 'msg') in the 'msg' namespace -->
  <xsl:template match="msg:*">
    <xsl:element name="{local-name()}" namespace="http://someaddress.com/m2">
      <xsl:apply-templates select="@* | node()"/>
    </xsl:element>
  </xsl:template>

  <!-- Change 'msg' element into 'newmsg' -->
  <xsl:template match="msg:msg">
    <xsl:element name="newmsg" namespace="http://someaddress.com/m2" >
      <!-- Keep "ds" declaration on the root element -->
      <xsl:namespace name="ds" select="'http://www.w3.org/2000/09/xmldsig#'"/>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

スタイルシートが XML ソースに適用されると、次のように出力されます (Saxon 9.4 を使用)。

<newmsg xmlns="http://someaddress.com/m2" 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" 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>

これはまさにあなたが望むものではありませんが、近いです。ルート要素で既に宣言されているため、要素のxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"宣言はまだありません。これが署名処理の問題である場合、どのように対処すればよいかわかりません。calcnodexsi

于 2012-09-14T17:03:07.263 に答える