输入XML:
<root>
<output_getquerydata>
<query name="test">
<parameters>
<parameter name="id">TS1</parameter>
</parameters>
<results>
<record>
<column name="address">VAL1</column>
</record>
</results>
</query>
</output_getquerydata>
<output_getquerydata>
<query name="test">
<parameters>
<parameter name="id">TS2</parameter>
</parameters>
<results>
<record>
<column name="address">VAL2</column>
</record>
</results>
</query>
</output_getquerydata>
<node>
<CTO>
<id>TRFG2</id>
<order_number>TRFG2</order_number>
<PT>
<address>
<id>C248355-91862</id>
<code>T-48-KS-3659-SHELL BR</code>
</address>
<reference/>
<comment/>
</PT>
<DT>
<address>
<id>C1050692</id>
<code>C1050692</code>
</address>
<comment>This is a comment.</comment>
</DT>
<OLS>
<OL>
<id>TS1</id>
<PT/>
<DT>
<station>
<id>C1050692-01</id>
<code>C1050692-01</code>
<addressId>C1050692</addressId>
</station>
</DT>
</OL>
<OL>
<id>TS2</id>
<PT/>
<DT>
<station>
<id>C1050692-01</id>
<code>C1050692-01</code>
<addressId>C1050692</addressId>
</station>
</DT>
</OL>
</OLS>
</CTO>
</node>
</root>
当前XSL:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="CTO/PT/address"/>
<!--exclude-->
<xsl:template match="CTO/OLS/OL/PT">
<PT>
<addressId>
<!--each OL ID-->
<xsl:variable name="OLID">
<xsl:value-of select="/../OL/id"/>
</xsl:variable>
<!--select the column value where the query parameter ID matches the OL id-->
<xsl:value-of select="//output_getquerydata/query[parameters/parameter[@name='id']=$OLID]/results/record/column[@name='address']"/>
</addressId>
</PT>
</xsl:template>
<xsl:template match="output_getquerydata"/>
</xsl:stylesheet>
期望输出:
<node>
<!--1. copy everything-->
<CTO>
<id>TRFG2</id>
<order_number>TRFG2</order_number>
<!--2. exclude address tag here-->
<PT>
<reference/>
<comment/>
</PT>
<DT>
<address>
<id>C1050692</id>
<code>C1050692</code>
</address>
<comment>This is a comment.</comment>
</DT>
<OLS>
<OL>
<!--3. match OL ID-->
<id>TS1</id>
<PT>
<!--4. and add here the value from the outputquery result-->
<addressId>VAL1</addressId>
</PT>
<DT>
<station>
<id>C1050692-01</id>
<code>C1050692-01</code>
<addressId>C1050692</addressId>
</station>
</DT>
</OL>
<OL>
<id>TS2</id>
<PT>
<addressId>VAL2</addressId>
</PT>
<DT>
<station>
<id>C1050692-01</id>
<code>C1050692-01</code>
<addressId>C1050692</addressId>
</station>
</DT>
</OL>
</OLS>
</CTO>
</node>
目标是复制所有内容,然后执行以下操作:
1。排除cto/pt/address标记,不要在输出中复制它
2。对于每个ols/ol/id,在ols/ol/pt/addressid下添加query/results标记中的值,其中query/parameter id与ols/ol/id匹配。
在我的例子中,对于id=ts 1的ol,我需要在pt/addressid下添加val1的值(取自具有查询/参数id=ts1的查询/结果)。
我试图定义一个保存ol id的变量,然后xsl值将选择适当的查询。但我不确定我做错了什么,可能是因为模板匹配,这使我处于那个特定的位置,无法正确匹配。
有人能帮我吗?
谢谢您!