【问题标题】:Inset xslt missing element插入 xslt 缺少元素
【发布时间】:2012-07-09 03:51:18
【问题描述】:

我在 xml 和 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)天气元素是否可以从输入 XML 中获得(如果可用)(26%)我必须使用 XSLT 删除 %,那么输出将为 26。如果 XML 中没有元素(天气)它必须默认创建100。

我们可以在 XSLT 中做到这一点吗。

谁能帮帮我

【问题讨论】:

标签: xml xslt xslt-1.0 xslt-2.0


【解决方案1】:

请查看以下 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 2012-04-24
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-22
    相关资源
    最近更新 更多