【问题标题】:XSL copy everything and replaceXSL 复制所有内容并替换
【发布时间】:2018-05-27 19:39:24
【问题描述】:

输入 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 下添加来自查询/结果标签的值,其中查询/参数 ID 与 OLS/OL/ID 匹配。 就我而言,对于 ID= TS 1 的 OL,我需要在 PT/addressID 下添加 VAL1 的值(取自查询/参数 ID = TS1 的查询/结果)

我尝试定义一个变量来保存 OL ID,然后 XSL 值将选择适当的查询。但我不确定我做错了什么,可能是因为模板匹配,将我定位在那个特定位置并且无法正确匹配。

有人可以帮帮我吗?

谢谢!

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

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

    <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

    【讨论】:

    • 谢谢,这行得通。一个问题:我的输入以 开头,在我的输出中我只希望 成为主要的父标签,我该怎么做?
    • 添加模板&lt;xsl:template match="/root"&gt;&lt;xsl:apply-templates/&gt;&lt;/xsl:template&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    相关资源
    最近更新 更多