修改xls以查找任何现有属性,如果存在,则不要覆盖w / xsi:nil =" true"

时间:2018-02-07 22:48:40

标签: xslt

我有以下问题。我有一个xls样式表来添加xsi:nil =&#34; true&#34; null属性到我的xml数据中的空白节点。但是,如果一个节点已经有一个属性<test s:id"121"/>,那么xls会将其删除并覆盖w / <test xsi:nil="true"/>,认为它是空白的。我不想要这种行为,我只想要xsi:nil =&#34; true&#34;如果节点是真正空白<test/> w / no属性,则添加。有人可以帮我修改以下xls样式表,并使用适当的条件语句来跳过具有属性的此<test s:id"121"/>节点吗?属性可能会有所不同,如果任何类型的属性已经存在,我只需要忽略null属性插入。

是否也可以更改下面的xsl以查找此<test></test>之类的空白节点,并将其转换为<test xsi:nil="true"\>。现在它只能添加null属性,如果它采用这种格式<test/>两种方式都很好。 TNX。

覆盖现有节点属性的XLS示例:

<xsl:stylesheet version="1.0"`
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" indent="yes"/>

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

<xsl:template match="*[not(text())]">
<xsl:copy>
    <xsl:attribute name="xsi:nil">true</xsl:attribute>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

您只需在模板匹配中添加对属性的检查......

<xsl:template match="*[not(text()) and not(@*)]">
  <xsl:copy>
    <xsl:attribute name="xsi:nil">true</xsl:attribute>
  </xsl:copy>
</xsl:template>

您可能希望准确定义空的定义。例如,使用此当前模板匹配,a元素在以下XML中将被视为空,因为a没有子文本节点或属性。

<a><b>Hello</b></a>

所以,也许模板匹配应该是这样的....?

<xsl:template match="*[not(text()) and not(*) and not(@*)]">