简体   繁体   中英

how can i create an xml file (copy) using only xslt

I would like to create a copy of an xml file using only xslt.

this is the xml file :

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet href="fichier_xslt_ecrire.xsl" type="text/xsl"?>
<tool>
  <field id="prodName">
    <value>HAMMER HG2606</value>
  </field>
  <field id="prodNo">
    <value>32456240</value>
  </field>
  <field id="price">
    <value>$30.00</value>
  </field>
</tool>

this is the xsl file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes"/>
  <xsl:template match="@* | node()">     
      <xsl:result-document method="xml" href="Copy_of_product_{@id}-output.xml">
       <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:result-document>
  </xsl:template>
</xsl:stylesheet>

But the problem is, when i open the xml file in a navigator, i dont get the second (the copy) file created. So how can i run this creation ? do i need any runtime engin or something ? Because i want to copy the xml file without any other tools. Thanks for your help.

if you want just copy that file in another document than

XSLT

<?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" indent="yes"/>
        <xsl:template match="/">     
            <xsl:result-document method="xml" href="Copy_of_product_{//@id}-output.xml">
                <xsl:copy>
                    <xsl:apply-templates/>
                </xsl:copy>
            </xsl:result-document>
        </xsl:template>
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"></xsl:apply-templates>
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>

and if you write a spilitter than use

XSLT

<xsl:template match="field">     
    <xsl:result-document method="xml" href="Copy_of_product_{@id}-output.xml">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:result-document>
</xsl:template>
<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"></xsl:apply-templates>
    </xsl:copy>
</xsl:template>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM