2016-07-24 2 views
0

XML에서 XML로 변환시 XSLT를 처음 사용했습니다. 조건에 따라 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과 같은 출력 *"Really? 따라서 시나리오 # 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>