在后续元素有值的情况下,如何将空字段添加到前面的兄弟元素?请参见示例 [英] How to add null fields to preceding sibling elements where the subsequent elements do have a value? See example
本文介绍了在后续元素有值的情况下,如何将空字段添加到前面的兄弟元素?请参见示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下XML:
数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假"> <?xml version="1.0" encoding="UTF-8"?>
<LaborTaskInterface>
<LaborTask thing1="a" thing2="c" thing3="d" thing4="e" thing5="f"
thing6="g" thing7="h" thing8="i" thing9="j">
<ltOverride unit_id="1" value="1" thing2="k" thing3="c" thing4="d" thing10="o"/>
<ltOverride unit_id="2" value="1" thing2="l" thing3="c" thing4="d" thing11="p"/>
<ltOverride unit_id="3" value="1" thing2="m" thing3="c" thing4="d" thing12="q"/>
<ltOverride unit_id="4" value="1" thing2="n" thing3="c" thing4="d" thing13="r"/>
</LaborTask>
<LaborTask thing1="aa" thing2="bb" thing3="dd" thing4="ee" thing5="ff"
thing6="gg" thing7="hh" thing8="ii" thing9="jj">
<ltOverride unit_id="11" value="11" thing2="kk" thing3="cc" thing4="dd" thing10="oo"/>
<ltOverride unit_id="22" value="12" thing2="ll" thing3="cc" thing4="dd" thing11="pp"/>
<ltOverride unit_id="33" value="13" thing2="mm" thing3="cc" thing4="dd" thing12="qq"/>
<ltOverride unit_id="44" value="14" thing2="nn" thing3="cc" thing4="dd" thing13="rr"/>
</LaborTask>
</LaborTaskInterface>
我目前能够使用以下XSLT-1.0将其转换并导入到Microsoft Access中:
数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假"><?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:key name="group" match="ltOverride/*" use="concat(generate-id(..), '|', name())"/>
<xsl:template match="LaborTask">
<xsl:variable name="temp-data">
<xsl:apply-templates select="ltOverride" mode="attributes-to-elements"/>
</xsl:variable>
<xsl:for-each select="msxsl:node-set($temp-data)/ltOverride">
<xsl:copy>
<xsl:for-each select="*[generate-id() = generate-id(key('group', concat(generate-id(..), '|', name()))[1])]">
<xsl:for-each select="key('group', concat(generate-id(..), '|', name()))">
<xsl:variable name="index">
<xsl:if test="position() > 1">
<xsl:value-of select="concat('.', position())"/>
</xsl:if>
</xsl:variable>
<xsl:element name="{name()}{$index}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:for-each>
</xsl:copy>
</xsl:for-each>
</xsl:template>
<xsl:template match="ltOverride" mode="attributes-to-elements">
<xsl:copy>
<xsl:apply-templates select="../@* | @*" mode="attributes-to-elements"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*" mode="attributes-to-elements">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
这将提供以下输出:
数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假"><LaborTaskInterface>
<ltOverride>
<thing1>a</thing1>
<thing2>c</thing2>
<thing2.2>k</thing2.2>
<thing3>d</thing3>
<thing3.2>c</thing3.2>
<thing4>e</thing4>
<thing4.2>d</thing4.2>
<thing5>f</thing5>
<thing6>g</thing6>
<thing7>h</thing7>
<thing8>i</thing8>
<thing9>j</thing9>
<unit_id>1</unit_id>
<value>1</value>
<thing10>o</thing10>
</ltOverride>
<ltOverride>
<thing1>a</thing1>
<thing2>c</thing2>
<thing2.2>l</thing2.2>
<thing3>d</thing3>
<thing3.2>c</thing3.2>
<thing4>e</thing4>
<thing4.2>d</thing4.2>
<thing5>f</thing5>
<thing6>g</thing6>
<thing7>h</thing7>
<thing8>i</thing8>
<thing9>j</thing9>
<unit_id>2</unit_id>
<value>1</value>
<thing11>p</thing11>
</ltOverride>
<ltOverride>
<thing1>a</thing1>
<thing2>c</thing2>
<thing2.2>m</thing2.2>
<thing3>d</thing3>
<thing3.2>c</thing3.2>
<thing4>e</thing4>
<thing4.2>d</thing4.2>
<thing5>f</thing5>
<thing6>g</thing6>
<thing7>h</thing7>
<thing8>i</thing8>
<thing9>j</thing9>
<unit_id>3</unit_id>
<value>1</value>
<thing12>q</thing12>
</ltOverride>
<ltOverride>
<thing1>a</thing1>
<thing2>c</thing2>
<thing2.2>n</thing2.2>
<thing3>d</thing3>
<thing3.2>c</thing3.2>
<thing4>e</thing4>
<thing4.2>d</thing4.2>
<thing5>f</thing5>
<thing6>g</thing6>
<thing7>h</thing7>
<thing8>i</thing8>
<thing9>j</thing9>
<unit_id>4</unit_id>
<value>1</value>
<thing13>r</thing13>
</ltOverride>
<ltOverride>
<thing1>aa</thing1>
<thing2>bb</thing2>
<thing2.2>kk</thing2.2>
<thing3>dd</thing3>
<thing3.2>cc</thing3.2>
<thing4>ee</thing4>
<thing4.2>dd</thing4.2>
<thing5>ff</thing5>
<thing6>gg</thing6>
<thing7>hh</thing7>
<thing8>ii</thing8>
<thing9>jj</thing9>
<unit_id>11</unit_id>
<value>11</value>
<thing10>oo</thing10>
</ltOverride>
<ltOverride>
<thing1>aa</thing1>
<thing2>bb</thing2>
<thing2.2>ll</thing2.2>
<thing3>dd</thing3>
<thing3.2>cc</thing3.2>
<thing4>ee</thing4>
<thing4.2>dd</thing4.2>
<thing5>ff</thing5>
<thing6>gg</thing6>
<thing7>hh</thing7>
<thing8>ii</thing8>
<thing9>jj</thing9>
<unit_id>22</unit_id>
<value>12</value>
<thing11>pp</thing11>
</ltOverride>
<ltOverride>
<thing1>aa</thing1>
<thing2>bb</thing2>
<thing2.2>mm</thing2.2>
<thing3>dd</thing3>
<thing3.2>cc</thing3.2>
<thing4>ee</thing4>
<thing4.2>dd</thing4.2>
<thing5>ff</thing5>
<thing6>gg</thing6>
<thing7>hh</thing7>
<thing8>ii</thing8>
<thing9>jj</thing9>
<unit_id>33</unit_id>
<value>13</value>
<thing12>qq</thing12>
</ltOverride>
<ltOverride>
<thing1>aa</thing1>
<thing2>bb</thing2>
<thing2.2>nn</thing2.2>
<thing3>dd</thing3>
<thing3.2>cc</thing3.2>
<thing4>ee</thing4>
<thing4.2>dd</thing4.2>
<thing5>ff</thing5>
<thing6>gg</thing6>
<thing7>hh</thing7>
<thing8>ii</thing8>
<thing9>jj</thing9>
<unit_id>44</unit_id>
<value>14</value>
<thing13>rr</thing13>
</ltOverride>
</LaborTaskInterface>
<LaborTaskInterface>
<ltOverride>
<thing1>a</thing1>
<thing2>c</thing2>
<thing2.2>k</thing2.2>
<thing3>d</thing3>
<thing3.2>c</thing3.2>
<thing4>e</thing4>
<thing4.2>d</thing4.2>
<thing5>f</thing5>
<thing6>g</thing6>
<thing7>h</thing7>
<thing8>i</thing8>
<thing9>j</thing9>
<unit_id>1</unit_id>
<value>1</value>
<thing10>o</thing10>
<thing11></thing11>
<thing12></thing12>
<thing13></thing13>
</ltOverride>
<ltOverride>
<thing1>a</thing1>
<thing2>c</thing2>
<thing2.2>l</thing2.2>
<thing3>d</thing3>
<thing3.2>c</thing3.2>
<thing4>e</thing4>
<thing4.2>d</thing4.2>
<thing5>f</thing5>
<thing6>g</thing6>
<thing7>h</thing7>
<thing8>i</thing8>
<thing9>j</thing9>
<unit_id>2</unit_id>
<value>1</value>
<thing10></thing10>
<thing11>p</thing11>
<thing12></thing12>
<thing13></thing13>
</ltOverride>
<ltOverride>
<thing1>a</thing1>
<thing2>c</thing2>
<thing2.2>m</thing2.2>
<thing3>d</thing3>
<thing3.2>c</thing3.2>
<thing4>e</thing4>
<thing4.2>d</thing4.2>
<thing5>f</thing5>
<thing6>g</thing6>
<thing7>h</thing7>
<thing8>i</thing8>
<thing9>j</thing9>
<unit_id>3</unit_id>
<value>1</value>
<thing10></thing10>
<thing11></thing11>
<thing12>q</thing12>
<thing13></thing13>
</ltOverride>
<ltOverride>
<thing1>a</thing1>
<thing2>c</thing2>
<thing2.2>n</thing2.2>
<thing3>d</thing3>
<thing3.2>c</thing3.2>
<thing4>e</thing4>
<thing4.2>d</thing4.2>
<thing5>f</thing5>
<thing6>g</thing6>
<thing7>h</thing7>
<thing8>i</thing8>
<thing9>j</thing9>
<unit_id>4</unit_id>
<value>1</value>
<thing10></thing10>
<thing11></thing11>
<thing12></thing12>
<thing13>r</thing13>
</ltOverride>
<ltOverride>
<thing1>aa</thing1>
<thing2>bb</thing2>
<thing2.2>kk</thing2.2>
<thing3>dd</thing3>
<thing3.2>cc</thing3.2>
<thing4>ee</thing4>
<thing4.2>dd</thing4.2>
<thing5>ff</thing5>
<thing6>gg</thing6>
<thing7>hh</thing7>
<thing8>ii</thing8>
<thing9>jj</thing9>
<unit_id>11</unit_id>
<value>11</value>
<thing10>oo</thing10>
<thing11></thing11>
<thing12></thing12>
<thing13></thing13>
</ltOverride>
<ltOverride>
<thing1>aa</thing1>
<thing2>bb</thing2>
<thing2.2>ll</thing2.2>
<thing3>dd</thing3>
<thing3.2>cc</thing3.2>
<thing4>ee</thing4>
<thing4.2>dd</thing4.2>
<thing5>ff</thing5>
<thing6>gg</thing6>
<thing7>hh</thing7>
<thing8>ii</thing8>
<thing9>jj</thing9>
<unit_id>22</unit_id>
<value>12</value>
<thing10></thing10>
<thing11>pp</thing11>
<thing12></thing12>
<thing13></thing13>
</ltOverride>
<ltOverride>
<thing1>aa</thing1>
<thing2>bb</thing2>
<thing2.2>mm</thing2.2>
<thing3>dd</thing3>
<thing3.2>cc</thing3.2>
<thing4>ee</thing4>
<thing4.2>dd</thing4.2>
<thing5>ff</thing5>
<thing6>gg</thing6>
<thing7>hh</thing7>
<thing8>ii</thing8>
<thing9>jj</thing9>
<unit_id>33</unit_id>
<value>13</value>
<thing10></thing10>
<thing11></thing11>
<thing12>qq</thing12>
<thing13></thing13>
</ltOverride>
<ltOverride>
<thing1>aa</thing1>
<thing2>bb</thing2>
<thing2.2>nn</thing2.2>
<thing3>dd</thing3>
<thing3.2>cc</thing3.2>
<thing4>ee</thing4>
<thing4.2>dd</thing4.2>
<thing5>ff</thing5>
<thing6>gg</thing6>
<thing7>hh</thing7>
<thing8>ii</thing8>
<thing9>jj</thing9>
<unit_id>44</unit_id>
<value>14</value>
<thing10></thing10>
<thing11></thing11>
<thing12></thing12>
<thing13>rr</thing13>
</ltOverride>
</LaborTaskInterface>