tags:

views:

14

answers:

1

Hi,everyone. I have a xml file here:

<DM_Function Function="attribute value">
  <DM>
    <DM_Source SourceID="id1" SourceTitle="Title1" SourceContent="content1">
    </DM_Source>
  </DM>
  <DM>
    <DM_Source SourceID="id2" SourceTitle="Title2" SourceContent="content2">
    </DM_Source>
  </DM>
  <DM>
    <DM_Source SourceID="id3" SourceTitle="Title3" SourceContent="content3">
    </DM_Source>
  </DM>
</DM_Function>

In the XSLT File ,there is a variable:

<xsl:param name="dmIndex" select="2" />

I want to transform the XML file into another.The variable is the element's index,i hope the other XML file just show the Root element and the specified element(including the attributes).

A: 

This transformation:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

  <xsl:param name="dmIndex" select="2" />   

 <xsl:template match="/*">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:copy-of select="DM[$dmIndex]"/>
    </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<DM_Function Function="attribute value">
  <DM>
    <DM_Source SourceID="id1" SourceTitle="Title1" SourceContent="content1">
    </DM_Source>
  </DM>
  <DM>
    <DM_Source SourceID="id2" SourceTitle="Title2" SourceContent="content2">
    </DM_Source>
  </DM>
  <DM>
    <DM_Source SourceID="id3" SourceTitle="Title3" SourceContent="content3">
    </DM_Source>
  </DM>
</DM_Function>

produces the wanted, correct result:

<DM_Function Function="attribute value">
   <DM>
      <DM_Source SourceID="id2" SourceTitle="Title2" SourceContent="content2"/>
   </DM>
</DM_Function>
Dimitre Novatchev
I get it.Thanks very much.
Howard Jia
@Howard-Jia: Glad this was useful. Then how about accepting my answer (by clicking on the check-mark sign near the answer)? :)
Dimitre Novatchev
Also `select="@*|DM[$dmIndex]"` will do the job.
Alejandro