这是与我之前提出的不同的问题。一位社区成员建议我提出一个新问题。
我需要 XSLT 1.0,因为 Microsoft Access 不接受任何其他版本
我有以下以属性为中心的 XML:
<?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>
</LaborTaskInterface>
我想得到以下结果:
*请注意,我需要 ItOverride 的每个实例的所有节点(无论是否为 null),如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<LaborTaskInterface>
<ltOverride>
<unit_id>1</unit_id>
<value>1</value>
<thing1>a</thing1>
<thing2>c</thing2>
<thing2[2]>k</thing2[2]>
<thing3>c</thing3>
<thing4>d</thing4>
<thing5>f</thing5>
<thing6>g</thing6>
<thing7>h</thing7>
<thing8>i</thing8>
<thing9>j</thing9>
<thing10>o</thing10>
<thing11></thing11>
<thing12></thing12>
<thing13></thing13>
</ltOverride>
<ltOverride>
<unit_id>2</unit_id>
<value>1</value>
<thing1>a</thing1>
<thing2>c</thing2>
<thing2[2]>l</thing2[2]>
<thing3>c</thing3>
<thing4>d</thing4>
<thing5>f</thing5>
<thing6>g</thing6>
<thing7>h</thing7>
<thing8>i</thing8>
<thing9>j</thing9>
<thing10></thing10>
<thing11>p</thing11>
<thing12></thing12>
<thing13></thing13>
</ltOverride>
<ltOverride>
<unit_id>3</unit_id>
<value>1</value>
<thing1>a</thing1>
<thing2>c</thing2>
<thing2[2]>m</thing2[2]>
<thing3>c</thing3>
<thing4>d</thing4>
<thing5>f</thing5>
<thing6>g</thing6>
<thing7>h</thing7>
<thing8>i</thing8>
<thing9>j</thing9>
<thing10></thing10>
<thing11></thing11>
<thing12>q</thing12>
<thing13></thing13>
</ltOverride>
<ltOverride>
<unit_id>4</unit_id>
<value>1</value>
<thing1>a</thing1>
<thing2>c</thing2>
<thing2[2]>n</thing2[2]>
<thing3>c</thing3>
<thing4>d</thing4>
<thing5>f</thing5>
<thing6>g</thing6>
<thing7>h</thing7>
<thing8>i</thing8>
<thing9>j</thing9>
<thing10></thing10>
<thing11></thing11>
<thing12>r</thing12>
<thing13></thing13>
</ltOverride>
</LaborTaskInterface>
您可以在上面看到 thing2 有两个实例,即 LaborTask 的实例和 ItOverride 的实例。我想保留每一个。因此,在每个 ItOverride 中,都会有 thing2 的两个实例(对命名约定选项开放)
此外,我希望能够在一个 XSLT 内完成此操作。
到目前为止,这是社区成员提供的 XSLT:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/LaborTaskInterface" >
<xsl:copy>
<xsl:for-each select="LaborTask/ltOverride">
<xsl:variable name="temp">
<dummy>
<xsl:copy-of select="../@*"/>
<xsl:copy-of select="@*"/>
</dummy>
</xsl:variable>
<xsl:copy>
<xsl:for-each select="$temp/dummy/@*">
<xsl:element name="{name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:for-each>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这让我很接近,结果是:
<?xml version="1.0" encoding="UTF-8"?>
<LaborTaskInterface>
<ltOverride>
<thing1>a</thing1>
<thing2>k</thing2>
<thing3>c</thing3>
<thing4>d</thing4>
<thing5>f</thing5>
<thing6>g</thing6>
<thing7>h</thing7>
<thing8>i</thing8>
<thing9>j</thing9>
<thing10>o</thing10>
<unit_id>1</unit_id>
<value>1</value>
</ltOverride>
<ltOverride>
<thing1>a</thing1>
<thing2>l</thing2>
<thing3>c</thing3>
<thing4>d</thing4>
<thing5>f</thing5>
<thing6>g</thing6>
<thing7>h</thing7>
<thing8>i</thing8>
<thing9>j</thing9>
<thing11>p</thing11>
<unit_id>2</unit_id>
<value>1</value>
</ltOverride>
<ltOverride>
<thing1>a</thing1>
<thing2>m</thing2>
<thing3>c</thing3>
<thing4>d</thing4>
<thing5>f</thing5>
<thing6>g</thing6>
<thing7>h</thing7>
<thing8>i</thing8>
<thing9>j</thing9>
<thing12>q</thing12>
<unit_id>3</unit_id>
<value>1</value>
</ltOverride>
<ltOverride>
<thing1>a</thing1>
<thing2>n</thing2>
<thing3>c</thing3>
<thing4>d</thing4>
<thing5>f</thing5>
<thing6>g</thing6>
<thing7>h</thing7>
<thing8>i</thing8>
<thing9>j</thing9>
<thing13>r</thing13>
<unit_id>4</unit_id>
<value>1</value>
</ltOverride>
</LaborTaskInterface>
我只有 XSLT 的基本知识,但很乐意提供任何其他信息来获得有效的答案!
我确实需要这种转换才能与 Microsoft Access 一起使用。
TYIA!
也许这样的东西适合你:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/LaborTaskInterface" >
<xsl:copy>
<xsl:for-each select="LaborTask/ltOverride">
<xsl:copy>
<xsl:for-each-group select="../@* | @*" group-by="name()">
<xsl:for-each select="current-group()">
<xsl:element name="{name()}{if (position() > 1) then concat('.', position()) else ''}">
<xsl:value-of select="." />
</xsl:element>
</xsl:for-each>
</xsl:for-each-group>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
在您给定的示例中,这将产生:
Result
<?xml version="1.0" encoding="UTF-8"?>
<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>
</LaborTaskInterface>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)