2012-07-09 80 views
-1

我在XML和XSLT插图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中做到这一点吗?

任何人都可以帮我在这里请

+1

诗,你的问题是相同的,你之前问一个(http://stackoverflow.com/questions/11375172/how-to-insert-a-missing-element-or-修改现有的元素/ 11375597#11375597),你已经给出了完美的答案。 – 2012-07-09 07:04:43

回答

0

请看看下面的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

+0

嗨@Ravish非常感谢你的解决方案:) – maha 2012-07-09 08:38:10