XSL复制一切并替换

时间:2018-05-27 10:20:26

标签: xml xslt

INPUT XML:

html pattern

CURRENT XSL:

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

目标是复制所有内容,然后执行以下操作: 1.排除CTO / PT /地址标签,不要将其复制到输出中 2.对于每个OLS / OL / ID,在OLS / OL / PT / addressID下添加查询/结果标记中的值,其中查询/参数ID与OLS / OL / ID匹配。 在我的情况下,对于ID = TS 1的OL,我需要在PT / addressID下添加VAL1的值(取自查询/带有查询/参数ID = TS1的结果)

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

有人可以帮帮我吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

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

<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