如何将 XSLT 转换应用于 XML 文件并生成另一个 XML? [英] How to apply XSLT transformations to XML file and produce another XML?

查看:44
本文介绍了如何将 XSLT 转换应用于 XML 文件并生成另一个 XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<output>
    <orders>
        <order>
            <id>1</id>
            <number>10002</number>
            <type>Loading</type>
            <date>2013-01-01T02:30:00</date>
        </order>
        <order>
            <id>2</id>
            <number>10003</number>
            <type>Loading</type>
            <date>2013-01-01T010:30:00</date>
        </order>

    </orders>
    <quantities>
        <quantity>
            <id_order>1</id_order>
            <unit>KG</unit>
            <value>1000</value>
        </quantity>
        <quantity>
            <id_order>2</id_order>
            <unit>PAL</unit>
            <value>3</value>
        </quantity>

    </quantities>
</output>

Output.xml 应该是这样的

Output.xml should look like this

<?xml version="1.0" encoding="ISO-8859-1"?>
<output>
    <orders>
        <order>
            <id>1</id>
            <number>10002</number>
            <type>Loading</type>
            <KG>1000</KG>
            <PAL>3</PAL>
            <M3>1.5</M3>
        </order>
        <order>
            <id>2</id>
            <number>10003</number>
            <type>Loading</type>
            <KG>2000</KG>
            <PAL>4</PAL>
        </order>

    </orders>
</output>

我需要申请的 XSLT 文件格式是什么?

What is the format of XSLT file I need to apply ?

ps:忽略这一行,sTu!"#id SO 要求我在这篇文章中写更多的文字,认为它是所有代码.忽略这一行,sTu!"#id SO 要求我在这篇文章中写更多的文字,认为它是所有代码.

ps: ignore this line, sTu!"#id SO asks me to write more text in this post thinking its all code. ignore this line, sTu!"#id SO asks me to write more text in this post thinking its all code.

推荐答案

以下 XSLT 满足您的需求(格式化除外):

The following XSLT does what you need (except formatting):

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes" encoding="iso-8859-1"/>

    <!-- Copy all elements recursively -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- Copy this element with subelements -->
    <xsl:template match="order">
        <!-- Save ID for queries -->
        <xsl:variable name="id" select="id" />
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
          <!-- Find all quantity elements by ID -->
          <xsl:for-each select="//quantity[id_order=$id]">
              <!-- Create element with name set to value of unit element and value of value element -->
              <xsl:element name="{unit}">
                  <xsl:value-of select="value"/>              
              </xsl:element>
          </xsl:for-each>
        </xsl:copy>
    </xsl:template>

    <!-- Skip these elements -->
    <xsl:template match="quantities" />
    <xsl:template match="date" />
</xsl:stylesheet>

结果

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <orders>
      <order>
         <id>1</id>
         <number>10002</number>
         <type>Loading</type>
         <KG>1000</KG>
      </order>
      <order>
         <id>2</id>
         <number>10003</number>
         <type>Loading</type>
         <PAL>3</PAL>
      </order>
   </orders>
</output>

这篇关于如何将 XSLT 转换应用于 XML 文件并生成另一个 XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆