Is there an easier way to copy image elements

2019-08-18 02:11发布

问题:

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.

回答1:

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>


回答2:

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>


标签: xml xslt