XSLT 转换以使用 cdata 部分创建 xml



我正在尝试使用其中的cdata部分进行xslt转换。我需要将转换后的 xml 放入 aXMLInput 标签作为 cdata section.iam 能够成功创建 xml。您能否告诉我如何在 XMLInput 中创建内容作为 cdata 部分。附加输入 xml,预期输出,xslt 和实际输出。谢谢阿达文斯。

输入 XML :

 <?xml version="1.0" encoding="UTF-8"?>
    <ns2:esbMessage xmlns:ns2="http://messagev2.esb.company.com/">
        <header>
            <identity>
                <master-id/>
                <source-id/>
                <user-id>calleruname</user-id>
            </identity>
            <esb-service-name>Pricing</esb-service-name>
            <source-system-id>caller</source-system-id>
            <message-type>REQ</message-type>
            <message-id>ID-GCSW01987-49931-1484306332450-6-131</message-id>
        </header>
        <body>
            <request>
                <esb-metadata>
                    <user-id>calleruname</user-id>
                    <service-name>Pricing</service-name>
                    <source-system-id>caller</source-system-id>
                </esb-metadata>
                <message-body>
                    <rating-request xmlns:urn1="urn:company:esb:services:Rating:v01">
                        <fo>
                            <policy>
                                <provider-input>
                                    <account-complexity code="med" description="Medium" source-system=" CUBE"/>
                                    <primary-sic-code>2052-cook and crackers</primary-sic-code>
                                    <policy-inception-date>2016-12-23</policy-inception-date>
                                    <policy-expiration-date>2017-12-23</policy-expiration-date>
                                    <requested-deductible>
                                        <amount>1000.00</amount>
                                        <currency>USD</currency>
                                    </requested-deductible>
                                    <requested-deductible>
                                        <amount>1000.00</amount>
                                        <currency>USD</currency>
                                    </requested-deductible>
                                </provider-input>
                            </policy>
                        </fo>
                    </rating-request>
                </message-body>
            </request>
        </body>
    </ns2:esbMessage>

预期输出:

<ns2:esbMessage xmlns:ns2="http://messagev2.esb.company.com/">
    <header>
        <identity>
            <master-id/>
            <source-id/>
            <user-id>calleruname</user-id>
        </identity>
        <esb-service-name>Pricing</esb-service-name>
        <source-system-id>caller</source-system-id>
        <message-type>REQ</message-type>
        <message-id>ID-GCSW01987-49931-1484306332450-6-131</message-id>
    </header>
    <body>
        <rate:companyrating xmlns:rate="http://rateservices.provider.com/">
            <aAddRoot>1</aAddRoot>
            <aAddInputs>0</aAddInputs>
            <aXMLInput><![CDATA[<rate lob="15">
                    <c i="0" desc="Policy">
                        <m i="2" n="PolicyInceptionDate" v="2016-12-23"/>
                        <m i="3" n="PolicyExpiryDate" v="2017-12-23"/>
                        <c i="1" desc="PropertyLine">
                            <m i="54" n="SICCode" v="2052-cook and crackers"/>
                            <m i="74" n="AccountSize" v="med"/>
                        </c>
                    </c>
                </rate>]]></aXMLInput>
        </rate:companyrating>
    </body>
</ns2:esbMessage>

XSLT :

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" xpath-default-namespace="yes"> </xsl:output>
    <xsl:template match="/">
        <xsl:apply-templates select="*"/>
    </xsl:template>
    <xsl:template match="body/request">
        <rate:companyrating xmlns:rate="http://rateservices.provider.com/">
            <aAddRoot>1</aAddRoot>
            <aAddInputs>0</aAddInputs>
            <xsl:apply-templates select="*"/>
        </rate:companyrating>
    </xsl:template>
    <xsl:template match="body/request/esb-metadata">
    </xsl:template>
    <xsl:template match="body/request/message-body">
        <aXMLInput>
            <rate lob="15">
                <c i="0" desc="Policy">
                    <xsl:call-template name="policy-inception-date"/>
                    <xsl:call-template name="policy-expiration-date"/>
                    <xsl:call-template name="primary-sic-code"/>
                </c>
            </rate>
        </aXMLInput>
    </xsl:template>
    <xsl:template name="policy-inception-date">
        <m>
            <xsl:attribute name="i"><xsl:value-of select="2"/></xsl:attribute>
            <xsl:attribute name="n"><xsl:value-of select="'PolicyInceptionDate'"/></xsl:attribute>
            <xsl:attribute name="v"><xsl:value-of select='rating-request/fo/policy/provider-input/policy-inception-date'/></xsl:attribute>
        </m>
    </xsl:template>
    <xsl:template name="policy-expiration-date">
        <m>
            <xsl:attribute name="i"><xsl:value-of select="3"/></xsl:attribute>
            <xsl:attribute name="n"><xsl:value-of select="'PolicyExpiryDate'"/></xsl:attribute>
            <xsl:attribute name="v"><xsl:value-of select='rating-request/fo/policy/provider-input/policy-expiration-date'/></xsl:attribute>
        </m>
    </xsl:template>
    <xsl:template name="primary-sic-code">
        <c i="1" desc="PropertyLine">
            <m>
                <xsl:attribute name="i"><xsl:value-of select="54"/></xsl:attribute>
                <xsl:attribute name="n"><xsl:value-of select="'SICCode'"/></xsl:attribute>
                <xsl:attribute name="v"><xsl:value-of select='rating-request/fo/policy/provider-input/primary-sic-code'/></xsl:attribute>
            </m>
            <m i="74" n="AccountSize" v="med"/>
        </c>
    </xsl:template>
    <xsl:template match="* | @*">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

实际输出:

<?xml version="1.0" encoding="UTF-8"?>
<ns2:esbMessage xmlns:ns2="http://messagev2.esb.company.com/">
    <header>
        <identity>
            <master-id/>
            <source-id/>
            <user-id>calleruname</user-id>
        </identity>
        <esb-service-name>Pricing</esb-service-name>
        <source-system-id>caller</source-system-id>
        <message-type>REQ</message-type>
        <message-id>ID-GCSW01987-49931-1484306332450-6-131</message-id>
    </header>
    <body>
        <rate:companyrating xmlns:rate="http://rateservices.provider.com/">
            <aAddRoot>1</aAddRoot>
            <aAddInputs>0</aAddInputs>
            <aXMLInput>
                <rate lob="15">
                    <c i="0" desc="Policy">
                        <m i="2" n="PolicyInceptionDate" v="2016-12-23"/>
                        <m i="3" n="PolicyExpiryDate" v="2017-12-23"/>
                        <c i="1" desc="PropertyLine">
                            <m i="54" n="SICCode" v="2052-cook and crackers"/>
                            <m i="74" n="AccountSize" v="med"/>
                        </c>
                    </c>
                </rate>
            </aXMLInput>
        </rate:companyrating>
    </body>
</ns2:esbMessage>
如果您

使用支持 XPath 3.0 serialize函数 (https://www.w3.org/TR/xpath-functions-30/#func-serialize) 的 Saxon 版本(如 9.6 或 9.7),则可以将模板更改为

<xsl:template match="body/request/message-body">
    <aXMLInput>
        <xsl:variable name="xml-data">
            <rate lob="15">
                <c i="0" desc="Policy">
                    <xsl:call-template name="policy-inception-date"/>
                    <xsl:call-template name="policy-expiration-date"/>
                    <xsl:call-template name="primary-sic-code"/>
                </c>
            </rate>             
        </xsl:variable>
        <xsl:value-of select="serialize($xml-data)"/>
    </aXMLInput>
</xsl:template>

并确保将

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

在旧版本的 Saxon 中,您可能能够使用扩展函数来执行类似的工作。

您需要将 cdata-section-elements 属性添加到输出标记中,如下所示:

    <xsl:output method="xml" cdata-section-elements="aXMLInput" version="1.0" encoding="UTF-8" indent="yes" xpath-default-namespace="yes"> </xsl:output>

这会将 aXMLInput 的内容包装为 CDATA。您可能需要将内容指定为/text()有关详细信息,请参阅 https://www.xmltutorial.info/xslt/how-to-output-cdata-with-xsl/

最新更新