插入 xslt 缺少元素 [英] Inset xslt missing element

查看:26
本文介绍了插入 xslt 缺少元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 xml 和 xslt 中有一个查询

I have a query in the xml and xslt

下面是输入的 XML

<?xml version="1.0" encoding="UTF-8"?>    
<Employer>
<Employees>
    <EmployeesDetails>van ind 26%</EmployeesDetails>
</Employees>    
<Employees>
    <EmployeesDetails>van ind</EmployeesDetails>
</Employees>    

下面是我的输出文件

<?xml version="1.0" encoding="UTF-8"?>
<Employer>
<Employees>
    <Names>van</Names>
    <Location>ind</Location>                
    <Weather>26</Weather>
</Employees>
<Employees>
    <Names>van</Names>
    <Location>ind</Location>
    <Weather>100</Weather>
</Employees>

在 XSLT 中,我需要检查(XSL:IF)天气元素是否可用,如果可用(26%)我必须使用 XSLT 删除 %,然后输出将是 26.如果 XML 中没有元素(天气)它必须默认创建 100.

In XSLT I need to check(XSL:IF)whether weather element is available or not from Input XML if available(26%) I have to remove % using XSLT then output will be 26. If there Is no element in XML(weather) It has to create by default 100.

我们是否可以在 XSLT 中做到这一点.

Can we able to do this in XSLT is that possible.

有人能帮我吗

推荐答案

请看下面的 XSLT.

Please have a look at the following XSLT.

<?xml version="1.0" encoding="utf-16"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes" />

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

    <xsl:template match="EmployeesDetails">
        <xsl:variable name="Detail" select="concat(., ' ')" />
        <xsl:variable name="Names" select="substring-before($Detail, ' ')" />
        <xsl:variable name="Location" select="substring-before(substring-after($Detail, concat($Names, ' ')), ' ')" />
        <xsl:variable name="Weather" select="substring-before(substring-after($Detail, concat($Location, ' ')), '% ')" />
        <Names>
            <xsl:value-of select="$Names" />
        </Names>
        <Location>
            <xsl:value-of select="$Location" />
        </Location>
        <Weather>
            <xsl:choose>
                <xsl:when test="string-length($Weather) &gt; 0">
                    <xsl:value-of select="$Weather" />
                </xsl:when>
                <xsl:otherwise>100</xsl:otherwise>
            </xsl:choose>
        </Weather>
    </xsl:template>
</xsl:stylesheet>

http://www.xmlplayground.com/Gg5F3z

这篇关于插入 xslt 缺少元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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