2

我有几条来自数据库的记录,用于文件中的相应记录。

示例记录号 XML

  1. <XML_FILE_HEADER file_name="sample.txt" />
  2. <XML_RECORD record_number="1" name="John Doe" Age="21"/>
  3. <XML_RECORD record_number="2" name""Jessica Sanchez" Age="23"/>
  4. <XML_FILE_FOOTER total_records="2"/>

现在对于每条记录,我都有一个 xslt 模板,可以在 xml 中创建输出文件。

对于记录 1:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt">
<xsl:output method="xml"/>
<xsl:template match="XML_FILE_HEADER">
    <xsl:element name="File">
    <xsl:attribute name="FileName"><xsl:value-of select="@file_name"/></xsl:attribute>
    </xsl:element>
  </xsl:element>
</xsl:template>
</xsl:stylesheet>

对于记录 2 和 3:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt">
     <xsl:output omit-xml-declaration="yes"/>
     <xsl:template match="XML_RECORD">
       <xsl:element name="Record">
        <xsl:attribute name="Name"><xsl:value-of select="@name"/></xsl:attribute>
        <xsl:element name="Details">
        <xsl:attribute name="Age"><xsl:value-of select="@Age"/></xsl:attribute>
        </xsl:element>
      </xsl:element>
     </xsl:template>
</xsl:stylesheet>

对于记录 4:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt">
     <xsl:output omit-xml-declaration="yes"/>
     <xsl:template match="XML_FILE_FOOTER">
       <xsl:element name="Totals">
        <xsl:attribute name="Total Records"><xsl:value-of select="@total_records"/></xsl:attribute>
      </xsl:element>
     </xsl:template>
</xsl:stylesheet>

问题是在使用上面的模板附加每条记录后,我会有一个输出:

<?xml version="1.0" encoding="UTF-8"?>
<File FileName="sample.txt"></File>
<Record Name="John Doe" Age="21"></Record>
<Record Name="Jessica Sanchez" Age="22"></Record>
<Totals Total Records="2"></Totals>

我如何能够在文件下插入记录和总计元素?这样它就会有这样的输出:

<?xml version="1.0" encoding="UTF-8"?>
<File FileName="sample.txt">
<Record Name="John Doe" Age="21"></Record>
<Record Name="Jessica Sanchez" Age="22"></Record>
<Totals Total Records="2"></Totals>
</File>

任何帮助将不胜感激。谢谢。

4

2 回答 2

2

就像这样简短易懂

<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:template match="/*">
  <xsl:apply-templates select="XML_FILE_HEADER"/>
 </xsl:template>

 <xsl:template match="XML_FILE_HEADER">
   <File FileName="{@file_name}">
     <xsl:apply-templates select="../*[not(self::XML_FILE_HEADER)]"/>
   </File>
 </xsl:template>

 <xsl:template match="XML_RECORD">
   <Record name="{@name}" Age="{@Age}"/>
 </xsl:template>

 <xsl:template match="XML_FILE_FOOTER">
   <Totals TotalRecords="{@total_records}"/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML(更正为格式正确)文档时:

<t>
    <XML_FILE_HEADER file_name="sample.txt" />
    <XML_RECORD record_number="1" name="John Doe" Age="21"/>
    <XML_RECORD record_number="2" name="Jessica Sanchez" Age="23"/>
    <XML_FILE_FOOTER total_records="2"/>
</t>

产生了想要的正确结果

<File FileName="sample.txt">
   <Record name="John Doe" Age="21"/>
   <Record name="Jessica Sanchez" Age="23"/>
   <Totals TotalRecords="2"/>
</File>

说明

  1. 正确使用模板。

  2. 正确使用xsl:apply-templates排序结果。

  3. 正确使用 AVT(属性值模板)。

  4. 避免使用xsl:element

  5. 没用xsl:call-template。_

  6. 几乎完全以“推式”实施。

于 2012-08-31T12:26:22.643 回答
0

你想要的是<xsl:call-template name="templatename" />元素。这允许您从另一个模板内部调用一个模板。

就像是

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt">
    <xsl:output method="xml"/>
    <xsl:template match="/XML_FILE/XML_FILE_HEADER">
      <xsl:element name="File">
        <xsl:attribute name="FileName">
          <xsl:value-of select="@file_name"/>
        </xsl:attribute>
        <xsl:for-each select="/XML_FILE/XML_RECORD">
          <xsl:call-template name="RecordTemplate" />
        </xsl:for-each>        
        <xsl:call-template name="TotalTemplate" />
      </xsl:element>
    </xsl:template>

    <xsl:template name="RecordTemplate">
      <xsl:element name="Record">
        <xsl:attribute name="Name"><xsl:value-of select="@name"/></xsl:attribute>
        <xsl:attribute name="Age"><xsl:value-of select="@Age"/></xsl:attribute>
      </xsl:element>
    </xsl:template>

    <xsl:template match="/XML_FILE/XML_FILE_FOOTER" name="TotalTemplate">
      <xsl:element name="Totals">
          <xsl:attribute name="Total Records"><xsl:value-of select="@total_records"/>
      </xsl:element>
    </xsl:template>
</xsl:stylesheet>

当然,您的输入必须是 XML 有效的(即有一个根节点),就像这样

<XML_FILE>
  <XML_FILE_HEADER file_name="sample.txt" />
  <XML_RECORD record_number="1" name="John Doe" Age="21"/>
  <XML_RECORD record_number="2" name""Jessica Sanchez" Age="23"/>
  <XML_FILE_FOOTER total_records="2"/>
</XML_FILE>
于 2012-08-31T09:07:09.330 回答