2014-02-11 211 views
0

我需要从某些xml中删除名称空间前缀,但使用XSLT保留名称空间。XSLT删除名称空间前缀,但使用JDK保留名称空间XSLTC

这正是这里描述 - How to remove namespace prefix leaving namespace value (XSLT)?

这时候我用XML文字编辑,但我在使用XSLTC工作的应用程序测试工作正常。不幸的是,它不适用于XSLTC。任何想法如何让它工作?

这里是源XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<ns0:eBooking xmlns:ns0="http://test.com"> 
    <ns0:eventInfo Id="ID00000000000000000020" version="1"> 
    <ns0:Event> 
     <ns0:tpAmb>1</ns0:tpAmb> 
     <ns0:procEmi>1</ns0:procEmi> 
    </ns0:Event> 
    </ns0:eventInfo> 
    <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> 
    <SignedInfo> 
     <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/> 
     <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/> 
     <Reference URI="#ID00000000000000000020"> 
     <Transforms> 
      <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/> 
      <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/> 
     </Transforms> 
     <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/> 
     <DigestValue>p42saeKIMnVR7/P/nzen8mTsq94=</DigestValue> 
     </Reference> 
    </SignedInfo> 
    <SignatureValue>SzLt....==</SignatureValue> 
    <KeyInfo> 
     <X509Data> 
     <X509Certificate>MIIHdzCCBV.......==</X509Certificate> 
     </X509Data> 
    </KeyInfo> 
    </Signature> 
</ns0:eBooking> 

这里是XSLT:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:ns0="http://test.com" 
       xmlns="http://test.com" > 
    <xsl:output indent="yes"/> 
    <xsl:template match="ns0:*"> 
     <xsl:element name="{local-name()}" > 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:element> 
    </xsl:template> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

当使用JDK XSLTC这给:

<?xml version="1.0" encoding="UTF-8"?> 
<eBooking> 
    <eventInfo Id="ID00000000000000000020" version="1"> 
    <Event> 
     <tpAmb>1</tpAmb> 
     <procEmi>1</procEmi> 
    </Event> 
    </eventInfo> 
    <Signature xmlns="http://www.w3.org/2000/09/xmldsig#" **xmlns:ns0="http://test.com"**> 
    <SignedInfo> 
     <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/> 
     <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/> 
     <Reference URI="#ID00000000000000000020"> 
     <Transforms> 
      <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/> 
      <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/> 
     </Transforms> 
     <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/> 
     <DigestValue>p42saeKIMnVR7/P/nzen8mTsq94=</DigestValue> 
     </Reference> 
    </SignedInfo> 
    <SignatureValue>SzLt....==</SignatureValue> 
    <KeyInfo> 
     <X509Data> 
     <X509Certificate>MIIHdzCCBV.......==</X509Certificate> 
     </X509Data> 
    </KeyInfo> 
    </Signature> 
</eBooking> 

,如果我与测试工作正常xml复制编辑器。

+1

那么XSLTC会发生什么?你会得到哪个结果? –

+0

最好添加一个源XML,一个所需的XML和其他相关信息,比如已经尝试过的XSLT。 – user3159253

+0

这里是源代码xml: – user3292737

回答

0

这看起来像是在您使用的XSLTC版本中的错误,它应该使用样式表中声明的默认名称空间。你或许可以解决这个问题有点了

<xsl:element name="{local-name()}" namespace="{namespace-uri()}" > 

至于你已经在Signature元素突出了xmlns:ns0,这其实是正确的行为,因为xsl:copy会将所有命名空间节点,当它复制的元素。为了摆脱这个,你需要另一个模板

<!-- higher priority than node() but lower than ns0:* --> 
<xsl:template match="*" priority="-0.4"> 
    <xsl:element name="{name()}" namespace="{namespace-uri()}" > 
    <xsl:apply-templates select="@*|node()" /> 
    </xsl:element> 
</xsl:template> 
相关问题