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 和 xslt 2.0 中执行

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

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

请帮我

提前致谢

4

2 回答 2

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>
于 2012-07-12T15:42:04.877 回答
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>
于 2012-07-12T16:56:53.443 回答