在xslt中添加更多逻辑



我的输入XML文件

<employerinfo id="2242">
<Employer>
 <Employeedetail>Sam SR Engineer 10%</Employeedetail>
 <Employeedetail>Sam SR 26%</Employeedetail>
 <Employeedetail>Sam SR</Employeedetail>
<Employeedetail>Sam GRTE</Employeedetail>
</Employer >
</employerinfo>

预期输出文件

<Employer>
<Employeedetails>
 <Employeedetail1>
  <Employeedetail>
   <Name>Sam</Name>
   <Grade>SR Engineer</Grade>
   <Experience>10</Experience>
  </Employeedetail>
  <Employeedetail>
   <Name>Sam</Name>
   <Grade>SR</Grade>
   <Experience>26</Experience>
 <Employeedetail>
   <Name>Sam</Name>
   <Grade>SR</Grade>
   <Experience>10</Experience>
</Employeedetail>
 <Employeedetail>
   <Name>Sam</Name>
   <Grade>GRTE</Grade>
   <Experience>10</Experience>
</Employeedetail >
</Employeedetail1>
</Employeedetails>
</Employer>

我试着在谷歌上搜索这种类型的XML如何在xslt1.0&xslt 2.0

其他几个StackOverflow用户也帮助了我,但这仍然是最后一个详细的XML输入。

如果我在XML中添加了额外的父节点,并且在XSLT中进行了修改,我将不会得到值

请帮我

提前感谢

这是我迄今为止的解决方案:

<?xml version='1.0' encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
    version='1.0'
    >
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="Employer">
<Employer>
<Employeedetails>
<Employeedetail1>
<xsl:apply-templates select="Employeedetail"/>
</Employeedetail1>
</Employeedetails>
</Employer>
</xsl:template>

<xsl:template match="Employeedetail">
<Employeedetail>
<xsl:variable name="s1" select="normalize-space(.)"/>
<xsl:variable name="name" select="substring-before($s1,' ')"/>
<Name><xsl:value-of select="$name"/></Name>
<xsl:variable name="s2" select="substring-after($s1,' ')"/>
<xsl:call-template name="t">
    <xsl:with-param name="s" select="$s2"/>
    <xsl:with-param name="grade"></xsl:with-param>
    <xsl:with-param name="exp"></xsl:with-param>
</xsl:call-template>
</Employeedetail>
</xsl:template>

<xsl:template name="t">
<xsl:param name="s"/>
<xsl:param name="grade"/>
<xsl:param name="exp"/>
<xsl:choose>
    <xsl:when test="contains($s,' ')">
        <xsl:call-template name="t">
            <xsl:with-param name="s" select="substring-after($s,' ')"/>
            <xsl:with-param name="grade"><xsl:value-of select="$grade"/><xsl:text> </xsl:text><xsl:value-of  select="substring-before($s,' ')"/></xsl:with-param>
            <xsl:with-param name="exp"><xsl:value-of  select="substring-after($s,' ')"/></xsl:with-param>
        </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
        <xsl:choose>
            <xsl:when  test="string-length($exp)&gt;0">
                <Grade><xsl:value-of select="normalize-space($grade)"/></Grade>
                <Experience><xsl:value-of select="$exp"/></Experience>
            </xsl:when>
            <xsl:otherwise>
                <Grade><xsl:value-of select="normalize-space($s)"/></Grade>
                <Experience>10</Experience>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

结果:

<Employer>
  <Employeedetails>
    <Employeedetail1>
      <Employeedetail>
        <Name>Sam</Name>
        <Grade>SR Engineer</Grade>
        <Experience>10%</Experience>
      </Employeedetail>
      <Employeedetail>
        <Name>Sam</Name>
        <Grade>SR</Grade>
        <Experience>26%</Experience>
      </Employeedetail>
      <Employeedetail>
        <Name>Sam</Name>
        <Grade>SR</Grade>
        <Experience>10</Experience>
      </Employeedetail>
      <Employeedetail>
        <Name>Sam</Name>
        <Grade>GRTE</Grade>
        <Experience>10</Experience>
      </Employeedetail>
    </Employeedetail1>
  </Employeedetails>
</Employer>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="Employer">
 <Employer>
  <Employeedetails>
   <Employeedetail1>
    <xsl:apply-templates/>
   </Employeedetail1>
  </Employeedetails>
 </Employer>
</xsl:template>
<xsl:template match="Employeedetail">
 <xsl:analyze-string select="." regex="^(.*) ((SR|GR).*?)( ([0-9]+)%)?$">
  <xsl:matching-substring>
   <Employeedetail>
    <Name><xsl:value-of select="regex-group(1)"/></Name>
    <Grade><xsl:value-of select="regex-group(2)"/></Grade>
    <Experience><xsl:value-of select="if(regex-group(5)) then regex-group(5) else 10"/></Experience>
   </Employeedetail>
  </xsl:matching-substring>
 </xsl:analyze-string>
</xsl:template>
</xsl:stylesheet>

相关内容

  • 没有找到相关文章

最新更新