使用XSLT1.0替换字符串



我有以下传入的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>

我需要从字符串中删除"-----开始证书-----"one_answers"-----结束证书-----"(忽略大小写)。后面和前面的"----------------"的数量可能会有所不同。

预期输出为:

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

请建议最好的方法。

我相信这应该适用于您:

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

我不知道你说的"忽略大小写";,虽然


编辑:

我已经设法找到了这种类型的文档(PEM文件)的规范。您将在第2节中看到,页眉和页脚的指定非常精确,直到BEGIN CERTIFICATEEND CERTIFICATE标签周围的连字符和空格的数量。

这意味着,对于一致的PEM文件,代码可以大大简化为:

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

相关内容

  • 没有找到相关文章

最新更新