【问题标题】:XSLT Convert each repetitive elements into unique recordsXSLT 将每个重复的元素转换为唯一的记录
【发布时间】:2014-11-25 04:05:47
【问题描述】:

我需要为项目和技能的每个重复元素创建一个唯一记录。尝试了我在 XSLT 中的已知选项,但没有得到正确的结果。

您能帮我为以下输入 XML 生成 XSLT。

输入 XML:

<root>
<Record>
<Emp_ID>288237</Emp_ID>
<Emp_Name>John</Emp_Name>
<Country>US</Country>
<Manager>Wills</Manager>
<Join_Date>5/12/2014</Join_Date>
<Experience>9 years</Experience>
<Project>abc</Project>
 <Skill>java</Skill>
<Project>def</Project>
<Skill>unix</Skill>
<Project>efg</Project>
<Skill>xml</Skill>
<Project>pqr</Project>
<Skill>sql</Skill>
<Project>xyz</Project>
<Skill>Analytics</Skill>
</Record>
</root>

所需的输出 XML:

 <root>
 <Record>
<Emp_ID>288237</Emp_ID>
<Emp_Name>John</Emp_Name>
<Country>US</Country>
<Manager>Wills</Manager>
<Join_Date>5/12/2014</Join_Date>
<Experience>9 years</Experience>
<Project>abc</Project>
<Skill>java</Skill>
</Record>
 <Record>
<Emp_ID>288237</Emp_ID>
<Emp_Name>John</Emp_Name>
<Country>US</Country>
<Manager>Wills</Manager>
<Join_Date>5/12/2014</Join_Date>
<Experience>9 years</Experience>
<Project>def</Project>
<Skill>unix</Skill>
</Record>
<Record>
<Emp_ID>288237</Emp_ID>
<Emp_Name>John</Emp_Name>
<Country>US</Country>
<Manager>Wills</Manager>
<Join_Date>5/12/2014</Join_Date>
<Experience>9 years</Experience>
<Project>efg</Project>
<Skill>xml</Skill>
</Record>
 <Record>
<Emp_ID>288237</Emp_ID>
<Emp_Name>John</Emp_Name>
<Country>US</Country>
<Manager>Wills</Manager>
<Join_Date>5/12/2014</Join_Date>
<Experience>9 years</Experience>
<Project>pqr</Project>
<Skill>sql</Skill>
</Record>
<Record>
<Emp_ID>288237</Emp_ID>
<Emp_Name>John</Emp_Name>
<Country>US</Country>
<Manager>Wills</Manager>
<Join_Date>5/12/2014</Join_Date>
<Experience>9 years</Experience>
<Project>xyz</Project>
<Skill>Analytics</Skill>
</Record>
</root>

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    这可以通过以下方式完成:

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="/">
        <root>
            <xsl:for-each select="root/Record/Project">
                <Record>
                    <xsl:copy-of select="../*[not(self::Project or self::Skill)]"/>
                    <xsl:copy-of select=". | following-sibling::Skill[1]"/>
                </Record>
            </xsl:for-each>
        </root>
    </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

      【解决方案2】:

      您可以使用以下 XSLT:

      <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output indent="yes" method="xml"  />
      <xsl:template match="root">
          <root>
              <xsl:for-each select="Record/Project">
                  <Record>
                  <xsl:copy-of select="../Emp_ID"/>
                  <xsl:copy-of select="../Emp_Name"/>
                  <xsl:copy-of select="../Country"/>
                  <xsl:copy-of select="../Join_Date"/>
                  <xsl:copy-of select="../Experience"/>
                  <xsl:copy-of select="."/>
                  <xsl:copy-of select="following::Skill[1]"/>
                  </Record>
              </xsl:for-each>
          </root>
      </xsl:template>
      
      </xsl:stylesheet>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-18
        • 1970-01-01
        • 2016-11-27
        • 1970-01-01
        • 2021-10-15
        • 1970-01-01
        • 2017-11-09
        相关资源
        最近更新 更多