XML文件由绝对同构的节点序列组成。遵循xml形式逻辑--没有父-子连接。所有节点都在同一级别上,它们是兄弟节点。所有节点包括:
所以它的结构看起来总是这样:
<document ID-1="value" ID-2="value" ID-3="value" attr-4="value"/>
<document ID-1="value" ID-2="value" ID-3="value" attr-4="value"/>
<document ID-1="value" ID-2="value" ID-3="value" attr-4="value"/>
<document ID-1="value" ID-2="value" ID-3="value" attr-4="value"/>
...etc但。尽管存在这种同质性,实际上,在包含在属性“值”中的数据级别上,仍然存在关于层次结构的信息,然后需要对这些信息进行解释。条件模型的虚拟层次结构:
建立联系的办法如下:
AIM:恢复每个节点内的所有层次结构链信息。技术上-对所有从属元素(子元素、子元素)添加所有“覆盖”元素的值属性。在建议的模型中,这意味着从相应的父节点和/或子父节点中添加(复制) attr-4 = "value"。简单地说,这意味着对子元素应该添加两个attr-4="value" (来自子父和父元素)。
1-来源:
<document ID-1="SunID" ID-2="NULL" ID-3="value" attr-4="SUN"/> <!-- this is parent's node -->
<document ID-1="EarthID" ID-2="SunID" ID-3="value" attr-4="EARTH" /> <!-- this is subparent -->
<document ID-1="value" ID-2="EarthID" ID-3="value" attr-4="Tokio"/> <!-- child-1 -->
<document ID-1="value" ID-2="EarthID" ID-3="value" attr-4="London"/> <!-- child-2 -->
<document ID-1="value" ID-2="EarthID" ID-3="value" attr-4="Rome"/> <!-- child-3 -->
<document ID-1="value" ID-2="EarthID" ID-3="value" attr-4="Cairo"/> <!-- child-4 -->2-XSLT-solution
我可以假设算法的以下要点可以在XSLT中实现:
(注意)这些表达式的潜在有用信息: ID-3值是所有xml文件中真正唯一的id。
3-输出(据称模型)
<document ID-1="SunID" ID-2="NULL" ID-3="value" attr-4="SUN"/> <!-- this is parent's date -->
<document ID-1="EarthID" ID-2="SunID" ID-3="value" attr-4="EARTH" attr-5="SUN"/> <!-- this is subparent -->
<document ID-1="value" ID-2="EarthID" ID-3="value" attr-4="Tokio" attr-5="EARTH" attr-6="SUN" /> <!-- child-1 -->
<document ID-1="value" ID-2="EarthID" ID-3="value" attr-4="London" attr-5="EARTH" attr-6="SUN" /> <!-- child-2 -->
<document ID-1="value" ID-2="EarthID" ID-3="value" attr-4="Rome" attr-5="EARTH" attr-6="SUN" /> <!-- child-3 -->
<document ID-1="value" ID-2="EarthID" ID-3="value" attr-4="Cairo" attr-5="EARTH" attr-6="SUN" /> <!-- child-4 -->的主要问题:XSLT代码看起来如何?upd:XSLT1.0中的澄清
(注)--当然,我们不知道父节点、子节点、子节点的确切位置。以及其属性值的内容。所有这些地球,太阳值必须是动态计算。
发布于 2019-12-22 21:01:38
即使在XSLT 1中,您也有可以定义和跟踪任何引用的键,使用key函数找到的元素,它只是一个递归的key:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:key name="ref" match="document" use="@ID-1"/>
<xsl:template match="document">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="key('ref', @ID-2)" mode="att"/>
</xsl:copy>
</xsl:template>
<xsl:template match="document" mode="att">
<xsl:param name="pos" select="count(@*) + 1"/>
<xsl:attribute name="attr-{$pos}">
<xsl:value-of select="@attr-4"/>
</xsl:attribute>
<xsl:apply-templates select="key('ref', @ID-2)" mode="att">
<xsl:with-param name="pos" select="$pos + 1"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/59447626
复制相似问题