假设以下xml输入。
<Report Name="Report2" >
<ITable i_id="Item ID" i_name="Item Name" heart_beat="Heart Beat" is_active="Is Active">
<FkItemID_Collection>
<FkItemID i_id="1">
<i_name i_name="80">
<heart_beat heart_beat="0">
<is_active is_active="True">
<Tags>
<Tag Name="ItemClass" Value="Division"/>
<Tag Name="ItemDisplayName" Value="80"/>
<Tag Name="ItemName" Value="80"/>
<Tag Name="ItemType" Value="Logical"/>
</Tags>
</is_active>
</heart_beat>
</i_name>
</FkItemID>
<FkItemID i_id="2">
//Same formatted day as above element
</FkItemID>
</FkItemID_Collection>
</ITable>
</Report>
关于XML的专长是可以有很多元素,比如 例如:
<i_name i_name="80">
<i_price i_price ="10">
<heart_beat heart_beat="0">`
同样。
可以动态更改此类元素的数量。标签始终位于<Tags>
块内。
作为最终结果,我期待像这样的输出
<Report Name="Report2">
<Table i_id="Item ID" i_name="item Name" heart_beat="heart_beat" col1="ItemClass" col2="ItemDisplayName" col3="ItemName" col4="ItemType" >
<Details agent_id="1" agent_name="80" col1="Division" col2="80" col3="80" col4="Logical" />
<Details i_id="2" i_name="BC" col1="Division" col2="BC" col3="BC" col4="Logical" />
</Table>
</Report>
我为该场景编写了一个XSLT。但我有一些问题,我无法解决。这是我到目前为止所开发的。我不知道我不知道我必须深入元素的部分,直到找到Tags块和如何迎合动态生成的标签,例如我上面提到的item_price。
这就是我现在所做的。
<xsl:template match="/*/*[local-name()='ITable']">
<xsl:element name="Table">
<xsl:for-each select="./*">
<xsl:for-each select="./*">
<xsl:if test="position()=1">
<xsl:attribute name="i_id">Item ID</xsl:attribute>
<xsl:attribute name="i_name">Item Name</xsl:attribute>
<xsl:attribute name="i_beat">heart_beat</xsl:attribute>
<xsl:for-each select="./*/*/*/*">
<xsl:attribute name="{concat('col', position())}">
<xsl:value-of select="@TagName"/>
</xsl:attribute>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
<xsl:for-each select="./*">
<xsl:element name="Details">
<xsl:attribute name="i_id">
<xsl:value-of select="@i_id"/>
</xsl:attribute>
<xsl:for-each select="./*">
<xsl:attribute name="i_name">
<xsl:value-of select="@i_name"/>
</xsl:attribute>
<xsl:for-each select="./*/*/*">
<xsl:attribute name="{concat('col', position())}">
<xsl:value-of select="@TagValue"/>
</xsl:attribute>
</xsl:for-each>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:for-each>
</xsl:element>
</xsl:template>
这个问题的最佳解决方案是什么。
答案 0 :(得分:0)
标签总是在一个区块内。 ... 我被这部分困住了 我不知道在找到之前我必须深入到元素内部的深度 标签块
这是后代轴的用途:descendant::Tags
或//Tags
将为您“找到”标记块,无论它有多深。