Вопрос

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).

Это было полезно?

Решение

This transformation:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <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>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top