2017-04-20 5 views
0

레코드 끝에 추가 요소를 추가해야합니다. 추가 된 요소의 형제는 선택적이거나 존재하지 않을 수 있습니다. 끝에 <Property> 요소가 추가되는 한. 첫 번째 시나리오는 <Property> 요소가 테스트 파일에 이미있는 경우 그대로 유지해야하지만 <User> 요소를 <Property> 요소 아래에 추가해야합니다. 두 번째 시나리오는 요소가없는 경우 끝에 <Property>을 추가하거나 <Data>의 모든 요소 뒤에 배치해야합니다. 다른 요소는 선택 사항입니다.XSLT에서 레코드 끝에 추가 요소를 추가하는 방법

나는이 추가 부분을 추가해야

<Property> 
    <User> 
     <Value listID="AAA">Sample testing</Value> 
    </User> 
</Property> 

1 시나리오 예 :<Property>는 testfile 위에 존재

INPUT

<Record> 
    <Data> 
     <Date>2017-04-25</Date> 
     <Name>John Kledd</Name> 
     <Address> 
     <BuildingNumber>4603</BuildingNumber> 
     </Address> 
     <Property> 
     <User> 
      <Value listID="123">Example Only</Value> 
     </User> 
     </Property> 
    </Data> 
</Record> 

E XPECTED OUTPUT

<Record> 
    <Data> 
     <Date>2017-04-25</Date> 
     <Name>John Kledd</Name> 
     <Address> 
     <BuildingNumber>4603</BuildingNumber> 
     </Address> 
     <Property> 
     <User> 
      <Value listID="123">Example Only</Value> 
     </User> 
     <User> 
      <Value listID="AAA">Sample testing</Value> 
     </User> 
     </Property> 
    </Data> 
</Record> 

2 시나리오 예 :<Property>는 testfile 위

INPUT

<Record 
    <Data> 
     <Date>2017-04-25</Date> 
     <Name>John Kledd</Name> 
     <Address> 
     <BuildingNumber>4603</BuildingNumber> 
     </Address> 
    </Data> 
</Record> 

예상 출력

012에 존재하지 않습니다 나는 <Address> 요소를 제거 할 경우 작동하지 않았다, 그러나, 내 XSLT는 두 시나리오에 대한 노력

<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"/> 
<!-- identity transform --> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 
<!-- This part deletes the Property element --> 
<xsl:template match="Data/Property"/> 
<xsl:template match="Data/Address"> 
    <xsl:copy-of select="."/> 
    <xsl:choose> 
     <xsl:when test="not(following-sibling::Property)"> 
      <xsl:element name="Property"> 
       <xsl:element name="User"> 
        <xsl:element name="Value"> 
         <xsl:attribute name="name">AAA</xsl:attribute> 
         <xsl:value-of select="'Sample Testing'"/> 
        </xsl:element> 
       </xsl:element> 
      </xsl:element> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:element name="Property"> 
       <xsl:element name="User"> 
        <xsl:element name="Value"> 
         <xsl:attribute name="name"><xsl:value-of select="../UserArea/Property/NameValue/@name"/></xsl:attribute> 
         <xsl:value-of select="../Property/User/Value"/> 
        </xsl:element> 
       </xsl:element> 
       <xsl:element name="User"> 
        <xsl:element name="Value"> 
         <xsl:attribute name="name">AAA</xsl:attribute> 
         <xsl:value-of select="'Sample Testing'"/> 
        </xsl:element> 
       </xsl:element> 
      </xsl:element> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
</xsl:stylesheet> 

XSLT : 여기

<Record> <Data> <Date>2017-04-25</Date> <Name>John Kledd</Name> <Address> <BuildingNumber>4603</BuildingNumber> </Address> <Property> <User> <Value listID="AAA">Sample testing</Value> </User> </Property> </Data> </Record> 

3,516,내가 두 시나리오에 사용되는 내 XSLT이다 . 너무 길면 XSLT v2.0을 사용하고 있습니다. 도와 줘서 고마워.

감사합니다,

답변

0

당신이 간단하게 할 수 없습니다 :

XSLT

<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="*"/> 

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

<xsl:variable name="default-user"> 
    <User> 
     <Value listID="AAA">Sample testing</Value> 
    </User> 
</xsl:variable> 

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

<xsl:template match="Property"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
     <xsl:copy-of select="$default-user"/> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
+0

는 michael.hor257k @ 정말 고마워요. – Charlotte

관련 문제