XSLT 1.0:根据值和变量复制除某些节点之外的所有内容 [英] XSLT 1.0: copy everything except certain nodes according to value and variable

查看:144
本文介绍了XSLT 1.0:根据值和变量复制除某些节点之外的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在系统环境中得到以下(简化的)XML:

I have the following (simplified) XML that I get in a system environment:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<IS_LOG>
    <USER>19291</USER>
    <DATE>2011-08-15</DATE>
    <TIME>15:36:36</TIME>
    <SYST>sy1</SYST>
    <MATERIALS>
        <item>
            <sy>100</sy>
            <mat>000000000000310000</mat>
        </item>
        <item>
            <sy>100</sy>
            <mat>000000000000491078</mat>
        </item>
    </MATERIALS>
</IS_LOG>
</root>

我工作的系统在运行时传递一个不包含在上述XML结构中的变量。

The system that I work with passes me a variable at runtime that is not included in the XML structure above.

我有以下XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" version="1.0">

<!-- System variable whose value I normally only get only at runtime;
for test purposes set locally -->
<xsl:variable name="SenderService" select="'AT'"/>

<xsl:template match="@*|node()">
    <xsl:choose>
        <xsl:when test="$SenderService='AT'">
            <xsl:copy>
                <xsl:apply-templates mode="AT" select="@*|node()"/>
            </xsl:copy>
        </xsl:when>
        <xsl:otherwise>
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
        </xsl:otherwise>
   </xsl:choose> 
</xsl:template>


<xsl:template mode="AT" match="item[mat &gt; 000000000000299999 and mat &lt; 000000000000399999]"/>

</xsl:stylesheet>

现在我需要复制所有元素 item 不包括 mat 在数字范围300000到399999和 SenderService 是'AT'的那些。
如果要在本地测试,请将我的XSLT中的 SenderService 更改为'Z',输出看起来不错,所有得到复制:

Now I need to copy all elements item excluding the ones where mat is in the number range of 300000 to 399999 and SenderService is 'AT'. If to test it locally I change the SenderService in my XSLT to e.g. 'Z', the output looks fine, all items get copied:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<IS_LOG>
    <USER>19291</USER>
    <DATE>2011-08-15</DATE>
    <TIME>15:36:36</TIME>
    <SYST>sy1</SYST>
    <MATERIALS>
        <item>
            <sy>100</sy>
            <mat>000000000000310000</mat>
        </item>
        <item>
            <sy>100</sy>
            <mat>000000000000491078</mat>
        </item>
    </MATERIALS>
</IS_LOG>
</root>

但是如果我将 SenderService 设置为'AT '输出如下所示:

But if I set SenderService to 'AT' the output looks like this:

    <?xml version="1.0" encoding="UTF-8"?><root>

    19291
    2011-08-15
    15:36:36
    sy1



            100
            000000000000491078



</root>

正确的项目被复制,但没有标签。有人有想法如何改变XSLT?

The correct item gets copied but without the tags. Has anyone an idea of how to change the XSLT?

感谢您的帮助,
Peter

Thank you for your help, Peter

推荐答案

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:variable name="SenderService" select="'AT'"/>

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


    <xsl:template match="item[mat &gt; 000000000000299999 and mat &lt; 000000000000399999]">
        <xsl:if test="$SenderService != 'AT'">
            <xsl:copy-of select="."/>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

这篇关于XSLT 1.0:根据值和变量复制除某些节点之外的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆