使用XSLT1.0替换字符串

时间:2016-05-04 20:55:00

标签: xslt xslt-1.0 pem

我有以下传入的XML输入。

    <Cert>
    <x509Cert>-----BEGIN CERTIFICATE----- MIIE7jCCA9agAwIBAgIQSxwJEMml4T/r5K/vfYKaJjANBgkqhkiG9w0BAQsFADB+ MQswCQYDVQQGEwJVUzEdMBsGA1UEChMUU3ltYW50ZWMgQ29ycG9yYXRpb24xHzAd BgNVBAsTFlN5bWFudGVjIFRydXN0IE5ldHdvcmsxLzAtBgNVBAMTJlN5bWFudGVj IENsYXNzIDMgU2VjdXJlIFNlcnZlciBDQSAtIEc0MB4XDTE1MDExMjAwMDAwMFoX DTE2MDExMzIzNTk1OVoweDELMAkGA1UEBhMCVVMxDTALBgNVBAgMBE9oaW8xETAP BgNVBAcMCENvbHVtYnVzMRcwFQYDVQQKDA5KUE1vcmdhbiBDaGFzZTEQMA4GA1UE CwwHR1RJIEdOUzEcMBoGA1UEAwwTZ25ld2ViLmpwbWNoYXNlLm5ldDCCASIwDQYJ KoZIhvcNAQEBBQADggEPADCCAQoCggEBAOh5cImc6o4dXMfRtlFGZxt16FajFDi1 gZ6A4eYnuU+PUa1gxC6RF4B8ENQdW66RNVHhgatOuGQ4DuphvREnHy32NTQCNGiy xYeUwAIsLDFPY0vc0I9c/x6MoIVqJKdesnfiA41R2t41w9viS2WtXhVc9cCYOwX7 pmGYVEelUxnUUd/HUZvY5AuCUAJFzQO66BbRYjRZvJDAvE2G+xJwFaB5IJo3dU/j j ZW0H9BeIQfRDlpxb4WL0cUS70FN1RyVDPZSwnyHej+BxEN3pjZXUryBjZ8R+Z5TE zbFusEBtC8qTuT+37wUOoMLB -----END CERTIFICATE-----</x509Cert>
    </Cert>

我需要从字符串中删除“----- BEGIN CERTIFICATE -----”和“----- END CERTIFICATE -----”(忽略大小写)。尾随和前置“----------”的数量可能会有所不同。

预期的输出是:

    <Cert>
    <x509Cert>MIIE7jCCA9agAwIBAgIQSxwJEMml4T/r5K/vfYKaJjANBgkqhkiG9w0BAQsFADB+ MQswCQYDVQQGEwJVUzEdMBsGA1UEChMUU3ltYW50ZWMgQ29ycG9yYXRpb24xHzAd BgNVBAsTFlN5bWFudGVjIFRydXN0IE5ldHdvcmsxLzAtBgNVBAMTJlN5bWFudGVj IENsYXNzIDMgU2VjdXJlIFNlcnZlciBDQSAtIEc0MB4XDTE1MDExMjAwMDAwMFoX DTE2MDExMzIzNTk1OVoweDELMAkGA1UEBhMCVVMxDTALBgNVBAgMBE9oaW8xETAP BgNVBAcMCENvbHVtYnVzMRcwFQYDVQQKDA5KUE1vcmdhbiBDaGFzZTEQMA4GA1UE CwwHR1RJIEdOUzEcMBoGA1UEAwwTZ25ld2ViLmpwbWNoYXNlLm5ldDCCASIwDQYJ KoZIhvcNAQEBBQADggEPADCCAQoCggEBAOh5cImc6o4dXMfRtlFGZxt16FajFDi1 gZ6A4eYnuU+PUa1gxC6RF4B8ENQdW66RNVHhgatOuGQ4DuphvREnHy32NTQCNGiy xYeUwAIsLDFPY0vc0I9c/x6MoIVqJKdesnfiA41R2t41w9viS2WtXhVc9cCYOwX7 pmGYVEelUxnUUd/HUZvY5AuCUAJFzQO66BbRYjRZvJDAvE2G+xJwFaB5IJo3dU/j j ZW0H9BeIQfRDlpxb4WL0cUS70FN1RyVDPZSwnyHej+BxEN3pjZXUryBjZ8R+Z5TE zbFusEBtC8qTuT+37wUOoMLB</x509Cert>
    </Cert>

请告知最好的方法。

1 个答案:

答案 0 :(得分:0)

我相信这对你有用:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="x509Cert">
    <xsl:copy>
        <xsl:value-of select="normalize-space(substring-after(substring-before(translate(., '-', ''), 'END CERTIFICATE'), 'BEGIN CERTIFICATE'))" />
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

我不确定你到底是什么意思&#34; 忽略案例&#34;但是。

编辑:

我已设法找到此类文档的specification(PEM文件)。您将在Section 2中看到页眉和页脚被精确指定为非常 - 低至BEGIN CERTIFICATEEND CERTIFICATE标签周围的连字符和空格的数量。

这意味着对于符合标准的PEM文件,代码可以大大简化为:

<xsl:template match="x509Cert">
    <xsl:copy>
        <xsl:value-of select="substring(., 29, string-length(.) - 54)" />
    </xsl:copy>
</xsl:template>