【问题标题】:How to apply XSLT transformations to XML file and produce another XML?如何将 XSLT 转换应用于 XML 文件并生成另一个 XML?
【发布时间】:2014-08-22 09:32:25
【问题描述】:

输入.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 应该是这样的

<?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文件?

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

【问题讨论】:

  • SO 并没有你想象的那么愚蠢。您的问题本质上是说“请为我编写代码,我懒得自己编写代码”。你非常幸运,有人回应了。我对这个问题投了反对票:以后,请解释一下您尝试过的方法以及卡住的地方。
  • 这个确切的问题已经在这里提出并回答:stackoverflow.com/questions/24064818。在这里提出并回答了一个非常相似的问题:stackoverflow.com/questions/24436431。看来您没有从这些答案中学到任何东西。或许您在使用 sTu 之前应该三思而后行!”#id 形容词?

标签: c# xml xslt transform


【解决方案1】:

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

<?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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 2021-08-12
    • 1970-01-01
    相关资源
    最近更新 更多