【问题标题】:使用 XSLT 文件将 XML 转换为 XML
【发布时间】:2019-04-11 09:16:33
【问题描述】:

我正在尝试将 XML 从一种格式转换为另一种格式,但在找到解释其工作原理的资源时运气不佳。如何使用其他 XML 标记内的值在 XML 标记内设置数据?

这是开始的 XML

<?xml version="1.0" encoding="utf-8"?>
<In xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="in.xsd">
  <Submit ID="1234">
    <Label>
      <Code>300</Code>
      <Source>27</Source>
    </Label>
    <Data>
      <Number>18</Number>
      <Date>2018-04-01</Date>
      <IsFile>0</IsFile>
      <Location></Location>
      <Files>
        <File>
          <Name>red.pdf</Name>
          <Classification>FILE</Classification>
        </File>
        <File>
          <Name>picture.pdf</Name>
          <Type>IMAGE</Type>
        </File>
      </Files>
    </Data>
  </Submit>
</In>

我当前的 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"/>

  <xsl:template match="/">
    <xsl:apply-templates select="In"/>
  </xsl:template>
  <xsl:template match="In">
    <Q xmlns:tns="Q" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Source="{@source}" Notification="true">
      <xsl:value-of select="Submit/Label/Source"/>
    </Q>
  </xsl:template>
</xsl:stylesheet>

使用 XSLT 转换 XML 文件后的预期结果。

<?xml version="1.0" encoding="utf-8"?>
<Q xmlns:tns="Q" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="scheme.xsd" Source="27" Notification="true">
<SubmitID="1234">
<Label>
    <Code>300</Code>
    <Source>27</Source>
</Label>
<Data>
    <Number>18</Number>
    <Date>2018-04-01</Date>
    <IsFile>0</IsFile>
</Data>
<Files>
    <File>
        <Name>red.pdf</Name>
        <Type>FILE</Type>
    </File>
    <File>
        <Name>picture.pdf</Name>
        <Type>IMAGE</Type>
    </File>
</Files>
</Submit>
</Q>

XSLT 不完整,不会产生预期的结果。我仍然缺少生成介于两者之间的代码。

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    试试下面的 XSLT-1.0 样式表:

    <?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"/>
    
      <!-- identity template - copies all elements and its children and attributes -->    
      <xsl:template match="node()|@*">
          <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
          </xsl:copy>
      </xsl:template>    
    
      <xsl:template match="/In">                   <!-- Remove the 'In' element -->
        <xsl:apply-templates select="node()"/>
      </xsl:template>
    
      <xsl:template match="Submit">                <!-- Create the 'Q' element and its sub-elements -->
        <Q xmlns:tns="Q" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Source="{@source}" Notification="true">
            <xsl:copy>
                <xsl:copy-of select="@*"/>
                <xsl:apply-templates select="Label" />
                <xsl:apply-templates select="Data" />
                <xsl:apply-templates select="Data/Files" />
            </xsl:copy>
        </Q>
      </xsl:template>
    
      <xsl:template match="Data">                  <!-- Create the 'Data' sub-element without all of its children -->
        <xsl:copy>
            <xsl:copy-of select="Number"/>
            <xsl:copy-of select="Date"/>
            <xsl:copy-of select="IsFile"/>
        </xsl:copy>
      </xsl:template>  
    
    </xsl:stylesheet>
    

    【讨论】:

    • 能够使用 {Label/Source} 正确设置源。这很棒。
    【解决方案2】:

    这将返回我需要的大部分内容。这是正确的做法吗?

    <?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"/>
    
      <xsl:template match="/">
        <xsl:apply-templates select="In"/>
      </xsl:template>
    
      <xsl:template match="In">
        <Q xmlns:tns="Q" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Source="{Submit/Label/Source}" Notification="true">
          <xsl:value-of select="Submit/Label/Source"/>
          <Label>
          <Code><xsl:value-of select="Submit/Label/Code"/></Code>
          <Source><xsl:value-of select="Submit/Label/Source"/></Source>
          </Label>
        </Q>
      </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 2014-11-15
      • 2016-08-24
      • 1970-01-01
      • 2011-12-25
      • 2019-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多