XSLT:将属性从一个节点复制到另一个节点

时间:2015-06-10 10:31:06

标签: xml xpath xslt-1.0

我对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下。预期结果是

<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来实现这个目标

2 个答案:

答案 0 :(得分:1)

您可以修改身份转换以完全复制除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>

根据要求。

注意:您是否希望所有@objectID属性 下面的某个visualChildren/object元素而不是 all < / em> 整个文档中的@objectID属性,然后更改

    <xsl:for-each select="//@objectID">

    <xsl:for-each select=".//@objectID">

答案 1 :(得分:0)

以这种方式尝试:

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">