我需要从XML中的PCDATA转换为另一种XML。任何人都可以帮助我吗?
我需要将业务线,网站位置和政策编号从请求映射到相应的字段作为响应。
输入包含转义XML的PCDATA的XML:
<ns2:Response xmlns:ns2="http://webservices.company.com/">
<return><ibdoc><result lob='15' env_def='SR_JBOSS_DEV' gen_date='2017-01-18 12:11:30 AM' ibdoc_version='3.1' engine_type='JBoss' site_location='S396AT092'>
<program parent_id='1001' program_id='1' program_ver='1' package_date='2017-01-05T13:06:01' status='PASS' gen_type='0' region_format='en-US' from_cache='true'>
<c i='0' d='Policy'>
<m i='POLICY NUMBER' d='POLICY NUMBER' v='1000001'/>
</c>
</program>
<stats>
<start_time>2017-01-18 12:11:30:0191 AM</start_time>
<stop_time>2017-01-18 12:11:30:0226 AM</stop_time>
<running_time>35</running_time>
<xml_walking>31</xml_walking>
</stats>
</result></ibdoc></return>
</ns2:Response>
XSLT:
<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="*"/>
<xsl:template match="/">
<ns2:custom-response xmlns:ns2="http://esbservices.company.com/">
</ns2:custom-response>
</xsl:template>
</xsl:stylesheet>
预期输出XML:
<ns2:custom-response xmlns:ns2="http://esbservices.company.com/">
<line-of-business>15</line-of-business>
<site>S396AT092</site>
<policy>
<policy-nr>1000001</policy-nr>
</policy>
</ns2:custom-response>
答案 0 :(得分:0)
使用XSLT 3.0和XPath 3.0 parse-xml
函数(在Saxon 9,Altova XMLSpy,oXygen中支持),您可以使用
<xsl:template match="/*">
<ns2:custom-response xmlns:ns2="http://esbservices.company.com/">
<xsl:variable name="return-doc" select="parse-xml(return)"/>
<xsl:apply-templates select="$return-doc/ibdoc/result"/>
</ns2:custom-response>
</xsl:template>
<xsl:template match="result">
<line-of-business>
<xsl:value-of select="@lob"/>
</line-of-business>
<site>
<xsl:value-of select="@site_location"/>
</site>
<policy>
<policy-nr>
<xsl:value-of select=".//c[@d = 'Policy']/m[@d = 'POLICY NUMBER']/@v"/>
</policy-nr>
</policy>
</xsl:template>