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

时间:2017-01-14 10:48:23

标签: xml xslt

Iam尝试使用cdata部分进行xslt转换。我需要将转换后的xml放入aXMLInput标签中作为cdata section.iam能够成功创建xml。您能告诉我如何在aXMLInput中创建内容as cdata section.Attached Input xml,Expected output,xslt和Actual output。 谢谢Adavance。

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

1 个答案:

答案 0 :(得分:0)

如果您使用支持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中,您可以使用扩展功能执行类似的工作。