我需要在记录的末尾添加一个额外的元素。添加元素的兄弟姐妹只是可选的,可以存在与否。只要在末尾添加<Property>
元素。有两种情况可以完成:第一种情况,如果测试文件中已经存在<Property>
元素,它应保持原样但我需要在{{1下添加<User>
元素的另一种情况元素。第二种情况,如果元素不存在,我需要在结尾处添加此<Property>
,或者应将其放在<Property>
的所有元素之后。其他元素只是可选的。
我需要添加这个附加部分:
<Data>
第一个方案示例: <Property>
<User>
<Value listID="AAA">Sample testing</Value>
</User>
</Property>
出现在测试文件中
INPUT
<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>
在测试文件中不存在
INPUT
<Property>
预期输出
<Record
<Data>
<Date>2017-04-25</Date>
<Name>John Kledd</Name>
<Address>
<BuildingNumber>4603</BuildingNumber>
</Address>
</Data>
</Record>
这是我用于两种情况的XSLT:
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>
我的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 v2.0,如果它太长则道歉。谢谢你的帮助。
此致
答案 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>