【发布时间】:2014-06-28 12:07:36
【问题描述】:
如果找到属性 1234 和 boolean1 值,则以下代码将 Y 或 N 分配给元素 ABC。 但有时属性 id 1234 根本不存在于 /Response/Specification/Section/item/Property 中。 所以元素 ABC 在输出 xml 中保持为空。 当属性 1234 不存在时,我想将 Element 值设置为 N。 我对 XSL 和 XML 很陌生。
输入 XML,其中每个规范标签包含多个部分和项目。每个部分和项目都有自己的 id。
<Response>
<Specification>
<Section>
<sectionid>9999</sectionid>
<item>
<itemid>8888</itemid>
<property>
<id>1234</id>
<property>ABC<property>
<boolean1>1</boolean1>
</property>
<property>
<id>5678</id>
<property>XYZ<property>
<boolean1>0</boolean1>
</property>
</property>
<property>
</property>
</item>
<item>
</item>
<item>
</item>
</Section>
<Section>
</Section>
<Section>
</Section>
</Specification>
</Response>
格式良好的输入 xml 的小样本。
<Response>
<Specification>
<Section>
<Sectionid>9999</Sectionid>
<Item>
<Itemid>8888</Itemid>
<Property>
<id>1234</id>
<Property>ABC</Property>
<Boolean1>1</Boolean1>
</Property>
<Property>
<id>5678</id>
<Property>XYZ</Property>
<Boolean1>0</Boolean1>
</Property>
</Item>
</Section>
</Specification>
</Response>
在原始帖子中,我删除了一些元素。我虽然它会使它变得简短而简单。下面是所有元素的更新 xsl
<ABC>
<xsl:for-each select="Response/Specification/Section/Item/Property[id=1234]">
<xsl:choose>
<xsl:when test="boolean1=1" >
<xsl:text>Y</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>N</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</ABC>
<XYZ>
<xsl:for-each select="Response/Specification/Section/Item/Property[id=5678]">
<xsl:choose>
<xsl:when test="boolean1=1" >
<xsl:text>Y</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>N</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</XYZ>
预期的输出 XML 仅有 parenttag,其中包含所有其他标签,如 ABC、XYZ、PQR 等。当属性 id 1234 不存在时,输出为空 ABC 标签。这里我们要添加 N 而不是空标签。
<parenttag>
</ABC>
<XYZ>N</XYZ>
</parenttag>
【问题讨论】: