Is there an easier way to copy image elements

2019-08-18 02:37发布

I am copying image elements from one XML file into a new one using XSLT. I am using the following template to copy an image element, but I think there is a simpler way to do this.

<xsl:apply-templates select="art_id"/>

<xsl:template match="art_id"><xsl:text>
</xsl:text><image><art_id>
<xsl:attribute name="href"><xsl:value-of select="@href"/></xsl:attribute>
</art_id></image>
</xsl:template>

The "new" XML image element is an exact copy of the original.

标签: xml xslt
2条回答
孤傲高冷的网名
2楼-- · 2019-08-18 02:42

Since you talked about making an "exact copy", xsl:copy-of would be an option.

<xsl:template match="art_id">
  <xsl:text>
</xsl:text>
  <image>
    <xsl:copy-of select="."/>
  </image>
</xsl:template>
查看更多
乱世女痞
3楼-- · 2019-08-18 03:06

Not sure if there is a simpler way, but if you use attribute value templates, you can do this:

<xsl:template match="art_id">
    <xsl:text>
</xsl:text>
    <image>
        <art_id href="{@href}"/>
    </image>
</xsl:template>
查看更多
登录 后发表回答