Xml 如何在XSLT中的记录末尾添加附加元素

Xml 如何在XSLT中的记录末尾添加附加元素,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我需要在记录的末尾添加一个额外的元素。所添加元素的同级只是可选的,可以存在也可以不存在。只要在末尾添加元素。有两种情况可以完成:第一种情况,如果测试文件中已经存在元素,它应该保持原样,但我需要在元素下添加另一个出现的元素。第二种情况,如果元素不存在,我需要在末尾添加此,或者它应该放在的所有元素之后。其他元素只是可选的 我需要添加此附加部分: <Property> <User> <Value listID="AAA">Sample test

我需要在记录的末尾添加一个额外的元素。所添加元素的同级只是可选的,可以存在也可以不存在。只要在末尾添加
元素。有两种情况可以完成:第一种情况,如果测试文件中已经存在
元素,它应该保持原样,但我需要在
元素下添加另一个出现的
元素。第二种情况,如果元素不存在,我需要在末尾添加此
,或者它应该放在
的所有元素之后。其他元素只是可选的

我需要添加此附加部分:

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

样品测试
第一个场景示例:
出现在测试文件中

输入

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

2017-04-25
约翰·克莱德
4603
仅举个例子
预期产出

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

2017-04-25
约翰·克莱德
4603
仅举个例子
样品测试
第二个场景示例:
不在测试文件中

输入

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

AAA
AAA
我的XSLT适用于这两种情况,但是,如果我删除
元素,它就不起作用。我使用的是XSLTv2.0,如果太长,请道歉。谢谢你的帮助


尊敬的,

您不能简单地:

XSLT

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

样品测试

非常感谢@michael.hor257k。