2016-07-24 91 views
0

我是XSLT的新手,负责XML到XML的转换。我想根据条件在xml中添加一个元素。如何使用XSLT在XML中插入新元素

我有这个员工的信息和要求是在每个员工的元素内添加标签。

**Scenario1** 
<Employee> 
<Name>Check1</Name> 
<Position> 
    <org> 
    <orgName>COMPANY</orgName> 
    <orgType>ABC</orgTyp> 
    <org> 
</Position> 
</Employee> 

**Scenario2** 
<Employee> 
<Name>Nitesh</Name> 
<Position> 
    <role>Consultant</role> 
</Position> 
</Employee> 

**Scenario3** 
<Employee> 
<Name>Nitesh</Name> 
</Employee> 

我写了下面的代码,但它没有给我想要的输出。

`

<xsl:when test="not(xs:Position)"> 
    <xsl:copy> 
     <!-- And everything inside it --> 
     <xsl:apply-templates select="@* | node()"/> 
     <!-- Add node --> 
     <xs:Position> 
      <xs:Organization> 
       <xs:Organization_Type>1<xsl:value-of select="$OrgType"/> 
       </xs:Organization_Type> 
       <xs:Organization_Code>2<xsl:value-of select="$OrgCode"/> 
       </xs:Organization_Code> 
       <xs:Organization_Name>3<xsl:value-of select="$OrgName"/> 
       </xs:Organization_Name> 
      </xs:Organization> 
     </xs:Position> 
    </xsl:copy> 
</xsl:when> 
<xsl:when test="xs:Position"> 
    <xsl:variable name="element" select="xs:Position"/> 
    <xsl:choose> 
     <xsl:when test="not(xs:Position/xs:Organization/xs:Organization_Type='COMPANY')"> 
      <xs:Organization> 
       <xs:Organization_Type>1<xsl:value-of select="$OrgType"/> 
       </xs:Organization_Type> 
       <xs:Organization_Code>2<xsl:value-of select="$OrgCode"/> 
       </xs:Organization_Code> 
       <xs:Organization_Name>3<xsl:value-of select="$OrgName"/> 
       </xs:Organization_Name> 
      </xs:Organization> 
      <xsl:copy-of select="$element"/> 
     </xsl:when> 
    </xsl:choose> 
</xsl:when>` 
+0

什么是每个方案预期的结果? –

+0

类似于情景1的输出 –

+0

“*类似情景1的输出*”真的吗?所以你不想在场景#2中输出'顾问'节点? –

回答

0

我不明白你试图代码 - ESP。因为它的主要部分不见了。

认为你想要做的事,如:

XSLT 1.0

<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:variable name="default-org"> 
    <org> 
     <orgName>default name</orgName> 
     <orgType>default type</orgType> 
    </org> 
</xsl:variable> 

<!-- identity transform --> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="Employee[not(Position)]"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
     <Position> 
      <xsl:copy-of select="$default-org"/> 
     </Position>  
    </xsl:copy> 
</xsl:template> 

<xsl:template match="Position[not(org)]"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
     <xsl:copy-of select="$default-org"/> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

当应用到下面的测试输入:

XML

<root> 
    <Employee> 
     <Name>A</Name> 
     <Position> 
     <org> 
      <orgName>COMPANY</orgName> 
      <orgType>ABC</orgType> 
     </org> 
     </Position> 
    </Employee> 
    <Employee> 
     <Name>B</Name> 
     <Position> 
     <role>Consultant</role> 
     </Position> 
    </Employee> 
    <Employee> 
     <Name>C</Name> 
    </Employee> 
</root> 

的结果将是:

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <Employee> 
     <Name>A</Name> 
     <Position> 
     <org> 
      <orgName>COMPANY</orgName> 
      <orgType>ABC</orgType> 
     </org> 
     </Position> 
    </Employee> 
    <Employee> 
     <Name>B</Name> 
     <Position> 
     <role>Consultant</role> 
     <org> 
      <orgName>default name</orgName> 
      <orgType>default type</orgType> 
     </org> 
     </Position> 
    </Employee> 
    <Employee> 
     <Name>C</Name> 
     <Position> 
     <org> 
      <orgName>default name</orgName> 
      <orgType>default type</orgType> 
     </org> 
     </Position> 
    </Employee> 
</root>