代码之家  ›  专栏  ›  技术社区  ›  user3529643

XSL复制所有内容并替换

  •  0
  • user3529643  · 技术社区  · 6 年前

    输入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值将选择适当的查询。但我不确定我做错了什么,可能是因为模板匹配,这使我处于那个特定的位置,无法正确匹配。

    有人能帮我吗?

    谢谢您!

    1 回复  |  直到 6 年前
        1
  •  2
  •   Martin Honnen    6 年前

    我将为交叉引用定义一个键:

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

    然后很容易得到这个值,剩下的你似乎做得很好:

    <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