XSLT - 为多值添加一个级别

时间:2021-06-05 07:33:49

标签: xslt

你能帮我创建一个代码来转换数据,如下所示。 我需要为条目中包含的数据添加一个级别。数据来自多值属性,但作为单行写入。我不知道该怎么做,寻求您的帮助。

我有这个代码,但它没有像我预期的那样工作。如果不知道 XLST 所以这就是我问的原因。

<entry>
 <c>PL</c>
 <cn>dackjo</cn>
 <department>FO</department>
 <givenName>Joe</givenName>
 <plant>Berlin</plant>
 <plant>Praga</plant>
 <sec>admin</sec>
 <sec>worker</sec>
 <ou>PL</ou>
 <sn>Dack</sn>
 <title>worker</title>
 <preferredLanguage>EN</preferredLanguage>
 <uid>u12c55cb--4efe</uid>
 <timezone>CET</timezone>
</entry>

我必须将上面的值转换为下面的格式。我们需要添加一个 在其后面对多值进行级别和分组。 预期输出:

 <entry>
  <c>PL</c>
  <cn>dackjo</cn>
  <department>FO</department>
  <givenName>Joe</givenName>
  <Plants>
     <plant>Berlin</plant>
     <plant>Praga</plant>
  </Plants>
  <SecRoles>
     <sec>admin</sec>
     <sec>admin</sec>
  </SecRoles>  
  <ou>PL</ou>
  <sn>Dack</sn>
  <title>worker</title>
  <preferredLanguage>EN</preferredLanguage>
  <uid>u12c55cb--4efe</uid>
  <timezone>CET</timezone>
 </entry>

2 个答案:

答案 0 :(得分:0)

可以使用以下方法生成与您相​​似但不完全相同的输出:

XSLT 2.0

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/entry">
    <xsl:copy>
        <xsl:copy-of select="* except plant"/>
        <Plants>
            <xsl:copy-of select="plant"/>
        </Plants>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

--
演示:https://xsltfiddle.liberty-development.net/bET2rWW

答案 1 :(得分:0)

我对此主题的解决方案。也许并不复杂,但目前可以使用。

<xsl:template match="entry">
<xsl:copy>
<xsl:copy-of select="UserName"/>
<xsl:copy-of select="isRetired" />
<xsl:copy-of select="Language"/>
<xsl:copy-of select="FirstName" />
<xsl:copy-of select="LastName"/>
<xsl:copy-of select="Email" />
<xsl:copy-of select="EmployeeCode"/>
<xsl:copy-of select="ExEmployee" />
<xsl:copy-of select="MobilePhone"/>
<xsl:copy-of select="JobTitle" />
<xsl:copy-of select="Location"/>
<Plants>
 <xsl:copy-of select="Plant" />
</Plants>
<Security>
 <xsl:copy-of select="Role" />
</Security>
</xsl:copy>
</xsl:template>

感谢您的帮助。

相关问题