這與我之前的問題不同。社區成員建議我提出一個新問題。
我需要 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 的每個實體的所有節點(無論是否為空),如下所示:
<?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 一起使用。
蒂亞!
uj5u.com熱心網友回復:
也許這樣的事情可以為你作業:
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>
在您給定的示例中,這將產生:
結果
<?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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/427385.html
標籤:xml 毫秒访问 xslt xslt-1.0 xslt 分组
上一篇:如何洗掉xslt<span>、<p>等中的富文本欄位標簽
下一篇:使用帶有xml的程序“sp_xml_preparedocument”在SQLServer中決議XML時默認命名空間無法決議
