在xslt中添加更多逻辑

时间:2012-07-12 13:17:19

标签: xml xslt xslt-1.0 xslt-2.0

我的输入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>

我尝试在Google中搜索这种类型的XML如何在xslt 1.0&amp; xslt 2.0

其他几个Stack Overflow用户也对我有所帮助。但这仍然是最终的细节输入XML。

如果我在XML中添加了额外的父节点,并且如果我在XSLT中修改了我没有得到值

请帮帮我

提前致谢

2 个答案:

答案 0 :(得分:0)

到目前为止,这是我的解决方案:

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

答案 1 :(得分:0)

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