我对XSLT比较陌生,我正在做一个涉及xml和xslt1.0的项目。
我有一个 xml 代码(简化版(,看起来像
<visualChildren>
<object class="com.zerog.ia.installer.InstallSet" >
<installChildren>
<object class="com.zerog.ia.installer.InstallBundle" objectID="33110emc908m">
<property></property>
</object>
<object class="com.zerog.ia.installer.InstallBundle" objectID="43110emc9667m">
<property></property>
</object>
</installChildren>
</object>
</visualChildren>
我需要迭代收集所有对象 ID 并存储为
<object RefId={ObjectId} />
在视觉儿童下。预期结果为
<visualChildren>
<object class="com.zerog.ia.installer.InstallSet" >
<installChildren>
<object class="com.zerog.ia.installer.InstallBundle" objectID="33110emc908m">
<property></property>
</object>
<object class="com.zerog.ia.installer.InstallBundle" objectID="43110emc9667m">
<property></property>
</object>
</installChildren>
</object>
<object RefId=33110emc908m />
<object RefId=43110emc9667m />
</visualChildren>
谁能帮助我使用 xslt 1.0 实现这一目标
您可以修改标识转换以完全复制除visualChildren/object
元素之外的所有内容,这些元素可以按原样复制以及您请求的 RefId
属性:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="visualChildren/object">
<xsl:copy>
<xsl:attribute name="RefId">
<xsl:for-each select="//@objectID">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:attribute>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
将上述 XSLT 应用于输入 XML:
<visualChildren>
<object class="com.zerog.ia.installer.InstallSet" >
<installChildren>
<object class="com.zerog.ia.installer.InstallBundle" objectID="33110emc908m">
<property></property>
</object>
<object class="com.zerog.ia.installer.InstallBundle" objectID="43110emc9667m">
<property></property>
</object>
</installChildren>
</object>
</visualChildren>
生成以下输出 XML:
<?xml version="1.0" encoding="UTF-8"?>
<visualChildren>
<object RefId="33110emc908m 43110emc9667m"
class="com.zerog.ia.installer.InstallSet">
<installChildren>
<object class="com.zerog.ia.installer.InstallBundle" objectID="33110emc908m">
<property/>
</object>
<object class="com.zerog.ia.installer.InstallBundle" objectID="43110emc9667m">
<property/>
</object>
</installChildren>
</object>
</visualChildren>
根据要求。
注意:如果您希望给定visualChildren/object
元素下的所有@objectID
属性,而不是整个文档中的所有@objectID
属性,请更改
<xsl:for-each select="//@objectID">
自
<xsl:for-each select=".//@objectID">
试试这个方式:
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"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/visualChildren">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xsl:for-each select=".//@objectID">
<object RefId="{.}"/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
注意:
如果要收集的对象始终在 installChildren
下,则替换:
<xsl:for-each select=".//@objectID">
随着效率更高:
<xsl:for-each select="object/installChildren/object/@objectID">