请参阅以下XML。在XML中,name元素是一个自关闭的空标记。我需要为此name元素标记添加一个文本值。这段XML代码可能在整个XML中重复多次。
<participant typeCode="LOC">
<participantRole classCode="SDLOC">
<id extension="00000000-0000-0000-0000-000000000000" root="1.0"/>
<addr nullFlavor="UNK"/>
<playingEntity>
<name/>
</playingEntity>
</participantRole>
</participant>
预期输出:需要为自关闭空名称元素标记添加UNK文本值。
<participant typeCode="LOC">
<participantRole classCode="SDLOC">
<id extension="00000000-0000-0000-0000-000000000000" root="1.0"/>
<addr nullFlavor="UNK"/>
<playingEntity>
<name>UNK</name>
</playingEntity>
</participantRole>
</participant>
我需要一个XSLT脚本来实现这个要求。
谢谢,
答案 0 :(得分:1)
您需要一个身份转换模板:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
加上名称标签的模板:
<xsl:template match="name[not(node())]">
<name>UNK</name>
</xsl:template>
在样式表标记中包装它,并添加一个xml标题:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="name[not(node())]">
<name>UNK</name>
</xsl:template>
</xsl:stylesheet>