2016-07-24 9 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のような出力*"本当ですか?したがって、シナリオ#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> 
関連する問題