2017-04-20 145 views
0

我需要在记录末尾添加一个附加元素。添加的元素的兄弟只是可选的,可以存在或不存在。只要<Property>元素将在最后添加。有两种情况可以完成:第一种情况,如果<Property>元素已经存在于测试文件中,它应该保持不变,但我需要在<Property>元素下添加另一个<User>元素。第二种情况下,如果元素不存在,我需要在末尾添加<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> 

Ë 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> 

第二方案例如:<Property>是不存在于testfile的

INPUT

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

预期输出

<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> 

这里是我的XSLT我使用这两种情况下:

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正在为这两种情况下,但是,如果我删除了<Address>元素没有奏效。我使用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

相关问题