我有一条消息
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns="http://xmlns.oracle.com/policyautomation/hub/12.0/metadata/types">
<soapenv:Header/>
<soapenv:Body>
<load-request root="Vehicles" region="en-US" language="en-US" timezone="Etc/GMT">
<tables>
<table name="Vehicles">
<link name="Cars" target="Car" />
</table>
</tables>
</load-request>
</soapenv:Body>
Run Code Online (Sandbox Code Playgroud)
我需要对其进行两次转换:
我知道如何变形加载请求,并尝试使用此解决方案来删除 SOAP,但无法设法将两者结合起来并删除信封并使用单个 xslt转换正文(加载请求)。结果 XML 应该是:
<load-request>
<root>Vehicles</root>
<region>en-US</region>
<language>en-US</language>
<timezone>Etc/GMT</timezone>
<request-context>
<parameter>
<name>MyParam1</name>
<value>MyValue</value>
</parameter>
</request-context>
<tables>
<table>
<name>Vehicles</name>
<link>
<name>Cars</name>
<target>Car</target>
</link>
</table>
</tables>
</load-request>
Run Code Online (Sandbox Code Playgroud)
我使用的 XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" >
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="soapenv:*">
<xsl:apply-templates select="@* | node()" />
</xsl:template>
<xsl:template match="load-request">
<xsl:element name="load-request">
<xsl:element name="root">
<xsl:value-of select="@root"/>
</xsl:element>
<xsl:element name="region">
<xsl:value-of select="@region"/>
</xsl:element>
<xsl:element name="language">
<xsl:value-of select="@language"/>
</xsl:element>
<xsl:element name="timezone">
<xsl:value-of select="@timezone"/>
</xsl:element>
<xsl:apply-templates select="request-context"/>
<xsl:apply-templates select="tables"/>
</xsl:element>
</xsl:template>
<xsl:template match="request-context">
<xsl:element name="request-context">
<xsl:for-each select="parameter">
<xsl:element name="parameter">
<xsl:element name="name">
<xsl:value-of select="@name"/>
</xsl:element>
<xsl:element name="value">
<xsl:value-of select="@value"/>
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template match="tables">
<xsl:element name="tables">
<xsl:for-each select="table">
<xsl:element name="table">
<xsl:element name="name">
<xsl:value-of select="@name"/>
</xsl:element>
<xsl:apply-templates select="link"/>
<xsl:for-each select="field">
<xsl:element name="field">
<xsl:element name="name">
<xsl:value-of select="@name"/>
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template match="link">
<xsl:element name="link">
<xsl:element name="name">
<xsl:value-of select="@name"/>
</xsl:element>
<xsl:element name="target">
<xsl:value-of select="@target"/>
</xsl:element>
</xsl:element>
</xsl:template>
<xsl:template match="field">
<xsl:for-each select="field">
<xsl:element name="field">
<xsl:element name="name">
<xsl:value-of select="@name"/>
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
更新:答案适用于输入。您能否权衡一下额外的调整:在我的一些场景中,将属性转换为元素是不够的。下面的留言
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns="http://xmlns.oracle.com/policyautomation/hub/12.0/metadata/types">
<soapenv:Header/>
<soapenv:Body>
<load-request root="Complains">
<field name="Explanation">
<text-val name="Text">The client needs a new toothbrush</text-val>
</field>
</load-request>
</soapenv:Body>
Run Code Online (Sandbox Code Playgroud)
需要成为
<load-request>
<root>Complains</root>
<field>
<name>Explanation</name>
<text-val>
<name>Text</name>
<value>The client needs a new toothbrush</value>
</text-val>
</field>
</load-request>
Run Code Online (Sandbox Code Playgroud)
从更简单的事情开始怎么样:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
exclude-result-prefixes="soapenv">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- remove all elements in the soapenv namespace -->
<xsl:template match="soapenv:*">
<xsl:apply-templates select="node()"/>
</xsl:template>
<!-- for the remaining elements (i.e. elements in the default namespace) ... -->
<xsl:template match="*">
<!-- ... create a new element with similar name in no-namespace -->
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<!-- convert attributes to elements -->
<xsl:template match="@*">
<xsl:element name="{local-name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
应用于您的示例input,固定格式良好(!):
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://xmlns.oracle.com/policyautomation/hub/12.0/metadata/types">
<soapenv:Header/>
<soapenv:Body>
<load-request root="Vehicles" region="en-US" language="en-US" timezone="Etc/GMT">
<tables>
<table name="Vehicles">
<link name="Cars" target="Car"/>
</table>
</tables>
</load-request>
</soapenv:Body>
</soapenv:Envelope>
Run Code Online (Sandbox Code Playgroud)
产生以下结果:
<?xml version="1.0" encoding="UTF-8"?>
<load-request>
<root>Vehicles</root>
<region>en-US</region>
<language>en-US</language>
<timezone>Etc/GMT</timezone>
<tables>
<table>
<name>Vehicles</name>
<link>
<name>Cars</name>
<target>Car</target>
</link>
</table>
</tables>
</load-request>
Run Code Online (Sandbox Code Playgroud)
回应您的编辑:
如果可以转换具有文本值的任何元素,以便文本值变成名为 的子元素value,则可以简单地向样式表添加另一个通用模板:
<xsl:template match="text()">
<value>
<xsl:value-of select="." />
</value>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
如果上述情况不正确,并且您需要显式寻址源 XML 中的特定元素,则需要在样式表中声明源的默认命名空间,为其分配前缀并在寻址元素时使用该前缀。的stylesheet元素,在这种情况下,应该是这样的:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tps="http://xmlns.oracle.com/policyautomation/hub/12.0/metadata/types"
exclude-result-prefixes="soapenv tps">
Run Code Online (Sandbox Code Playgroud)
并且您的模板将采用以下形式:
<xsl:template match="tps:text-val">
<text-val>
<!-- more instructions here -->
</text-val>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9027 次 |
| 最近记录: |