我有输入xml如下
<node>
<id>1234</id>
<value1>DoNoChange</value1>
<value2></value2>
<value3></value3>
</node>
现在,我将使用XSL将上述XML转换为低于一
<node>
<id>1234</id>
<value1>DoNoChange</value1>
<value2>NewValue2</value2>
<value2>NewValue3</value2>
</node>
哪个NewValue2和NewValue3是来自API调用的响应内容,例如http://example.com/api/getDataByID/1234
,它将返回响应为
<data>
<value2>NewValue2</value2>
<value3>NewValue3</value3>
<data>
你能告诉我如何为它构建XSL吗?
到目前为止我尝试过的是
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:param name="code" select="1234"/>
<xsl:variable name="endpoint" as="xs:string" select="'http://example.com/api/getDataByID/1'"/>
<!-- the http request element -->
<xsl:variable name="request">
<http-request method="get" mime-type="application/xml" content-type="application/xml">
</http-request>
</xsl:variable>
<xsl:template match="node/id">
<xsl:variable name="rest_response" select="ex:httpSend($request, $endpoint)"/>
<id><xsl:value-of select="$rest_response/data"/></id>
</xsl:template>
</xsl:stylesheet>
我正在努力做的逻辑是
谢谢,
如果您对API的调用返回XML响应,那么您应该能够执行以下操作:
XSLT 1.0
<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:template match="/node">
<xsl:copy>
<xsl:copy-of select="id | value1"/>
<xsl:variable name="response" select="document(concat('http://example.com/api/getDataByID/', id))" />
<value2>
<xsl:value-of select="$response/data/value2"/>
</value2>
<value3>
<xsl:value-of select="$response/data/value3"/>
</value3>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
未经测试,因为没有提供测试环境。