Inset xslt missing element

岁酱吖の 提交于 2019-12-13 10:28:39

问题


I have a query in the xml and xslt

The below is the Input XML

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

The below is my output file

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

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.

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

Can anyone help me out here please


回答1:


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



来源:https://stackoverflow.com/questions/11388891/inset-xslt-missing-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!