XSLT 如何组合模板并将现有字段添加到某些元素
Posted
技术标签:
【中文标题】XSLT 如何组合模板并将现有字段添加到某些元素【英文标题】:XSLT How to combine templates and add an existing field to certain elements 【发布时间】:2021-12-21 09:26:10 【问题描述】:我正在尝试更改 xml 文件,已尝试多次但无法获得任何有意义的输出。 转换后的 xml 文件应该是:
-
没有遮罩属性
版本和状态字段应从所有节点中删除
字段编号必须是记录、元素1、元素2、元素等的顶部字段。
任何帮助将不胜感激
这是我的 xml 输入:
<Objects>
<Record mask="1234qwer">
<reference>104006ALS</reference>
<version>9</version>
<status>NEW</status>
<createdTime>2018-06-18T09:37:48.560Z</createdTime>
<links>
<link mask="1234qwer12">
<reference>112045MS</reference>
<version>0</version>
<linkReason>Revision</linkReason>
<createdDateTime>2018-07-30T10:45:16.870Z</createdDateTime>
<lastModifiedDateTime>2018-07-30T10:45:16.870Z</lastModifiedDateTime>
<relevantDateTime>2018-07-30T10:45:16.870Z</relevantDateTime>
<element1 mask="eqwrwqe12">
<reference>17001</reference>
<version>1</version>
</element1>
</link>
<link mask="q0SM9">
<reference>109085</reference>
<version>0</version>
<linkReason>SomeReason</linkReason>
<createdDateTime>2018-05-18T10:50:08.520Z</createdDateTime>
<lastModifiedDateTime>2018-05-18T10:50:08.520Z</lastModifiedDateTime>
<relevantDateTime>2018-05-18T10:50:08.520Z</relevantDateTime>
<element2 mask="q0SM934232">
<reference>13001</reference>
<version>1</version>
<conditions>
<condition mask="q0SM921312">
<reference>9001</reference>
<version>1</version>
<conditionNumber>1</conditionNumber>
<conditionVariations/>
</condition>
</conditions>
</element2>
</link>
<link mask="yq0SM92">
<reference>109043</reference>
<version>0</version>
<element3 mask="yJq0SM92">
<reference>21006</reference>
<version>8</version>
<status>NEW</status>
<createdDateTime>2018-05-18T10:16:22.430Z</createdDateTime>
<links>
<link mask="yJq0SM9212">
<reference>112193</reference>
<version>1</version>
<fromDate>2018-07-31</fromDate>
<element4 mask="12yJq0SM9">
<reference>26004</reference>
<version>0</version>
<status>NEW</status>
<createdDateTime>2018-08-24T20:46:55.500Z</createdDateTime>
</element4>
</link>
</links>
</element3>
</link>
</links>
<Number>999999</Number>
</Record>
</Objects>
</AllObjects>
当前 xslt:
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @* "/>
</xsl:copy>
</xsl:template>
<xsl:template match="@mask"/>
<xsl:template match="version | status"/>
<xsl:template match="Record | Record/links/link/element1 | Record/links/link/element2 | Record/links/link/element3 | Record/links/link/element4">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="Number"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
期望的结果:
<Objects>
<Record>
<Number>999999</Number>
<reference>104006ALS</reference>
<createdTime>2018-06-18T09:37:48.560Z</createdTime>
<links>
<link>
<reference>112045MS</reference>
<linkReason>Revision</linkReason>
<createdDateTime>2018-07-30T10:45:16.870Z</createdDateTime>
<lastModifiedDateTime>2018-07-30T10:45:16.870Z</lastModifiedDateTime>
<relevantDateTime>2018-07-30T10:45:16.870Z</relevantDateTime>
<element1>
<Number>999999</Number>
<reference>17001</reference>
</element1>
</link>
<link>
<reference>109085</reference>
<linkReason>SomeReason</linkReason>
<createdDateTime>2018-05-18T10:50:08.520Z</createdDateTime>
<lastModifiedDateTime>2018-05-18T10:50:08.520Z</lastModifiedDateTime>
<relevantDateTime>2018-05-18T10:50:08.520Z</relevantDateTime>
<element2>
<Number>999999</Number>
<reference>13001</reference>
<conditions>
<condition>
<reference>9001</reference>
<conditionNumber>1</conditionNumber>
<conditionVariations/>
</condition>
</conditions>
</element2>
</link>
<link>
<reference>109043</reference>
<element3>
<Number>999999</Number>
<reference>21006</reference>
<createdDateTime>2018-05-18T10:16:22.430Z</createdDateTime>
<links>
<link>
<reference>112193</reference>
<fromDate>2018-07-31</fromDate>
<element4>
<Number>999999</Number>
<reference>26004</reference>
<createdDateTime>2018-08-24T20:46:55.500Z</createdDateTime>
</element4>
</link>
</links>
</element4>
</link>
</links>
<Number>999999</Number>
</Record>
</Objects>
</AllObjects>
【问题讨论】:
【参考方案1】:你快到了 - 试试吧:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@mask | version | status"/>
<xsl:template match="Record | element1 | element2 | element3 | element4">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:copy-of select="ancestor-or-self::Record/Number"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
如果(从您的示例看来)除了mask
之外没有其他属性,您可以将代码简化为:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="version | status"/>
<xsl:template match="Record | element1 | element2 | element3 | element4">
<xsl:copy>
<xsl:copy-of select="ancestor-or-self::Record/Number"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
【讨论】:
太棒了!感谢您的回复以上是关于XSLT 如何组合模板并将现有字段添加到某些元素的主要内容,如果未能解决你的问题,请参考以下文章