简体   繁体   English

XSL复制所有内容并替换

[英]XSL copy everything and replace

INPUT XML: 输入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>

CURRENT XSL: 当前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>

DESIRED OUTPUT: 期望的输出:

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

The goal is to copy everything and then do the following: 1. exclude the CTO/PT/address tag, do not copy it in the output 2. for each OLS/OL/ID add under OLS/OL/PT/addressID the value from the query/results tag, where the query/parameter ID matches the OLS/OL/ID. 目标是复制所有内容,然后执行以下操作:1.排除CTO / PT / address标记,请勿将其复制到输出中2.对于每个OLS / OL / ID,在OLS / OL / PT / addressID下添加值从查询/结果标签中查询/参数ID与OLS / OL / ID匹配。 In my case, for the OL with ID= TS 1, i need to add under PT/addressID the value of VAL1 (taken from the query/results that has the query/parameter ID = TS1) 在我的情况下,对于ID = TS 1的OL,我需要在PT / addressID下添加VAL1的值(取自具有查询/参数ID = TS1的查询/结果)

I've tried to define a variable that saves the OL ID and then the XSL value will select the appropriate query. 我试图定义一个保存OL ID的变量,然后XSL值将选择适当的查询。 But I am not sure what I am doing wrong, probably because of the template match, that positions me in that specific position and can't match properly. 但是我不确定我做错了什么,可能是由于模板匹配导致我处于该特定位置而无法正确匹配。

Can somebody please help me out? 有人可以帮我吗?

Thank you! 谢谢!

I would define a key for the cross-reference: 我将为交叉引用定义一个键:

<xsl:key name="query" match="output_getquerydata/query/results/record/column[@name = 'address']" use="ancestor::query/parameters/parameter[@name = 'id']"/>

then it is easy to pull in that value, the rest you seem to have done fine: 那么很容易获得该值,其余的似乎还不错:

<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:strip-space elements="*"/>

<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="CTO/PT/address"/>

<xsl:key name="query" match="output_getquerydata/query/results/record/column[@name = 'address']" use="ancestor::query/parameters/parameter[@name = 'id']"/>

<xsl:template match="CTO/OLS/OL/PT">
    <xsl:copy>
        <addressId>
            <xsl:value-of select="key('query', ../id)"/>
        </addressId>
    </xsl:copy>
</xsl:template>

<xsl:template match="output_getquerydata"/>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/eiZQaF9 https://xsltfiddle.liberty-development.net/eiZQaF9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM