在处理/评估每个模板后应用通用模板

时间:2014-01-02 09:06:56

标签: xml xslt

我有这个XSLT:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="no" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="Person">
    <xsl:apply-templates select ="/Person/FirstName"/>
    <xsl:apply-templates select ="/Person/MiddleName"/>
    <xsl:apply-templates select ="/Person/LastName"/>
    <xsl:apply-templates select ="/Person/CompanyInfo/CompanyName"/>
  </xsl:template>

  <xsl:template match="Person/FirstName">
    <FirstName>
      <xsl:value-of select="." />
    </FirstName>
  </xsl:template>
  <xsl:template match="Person/MiddleName">
    <MiddleName>
      <xsl:value-of select="." />
    </MiddleName>
  </xsl:template>
  <xsl:template match="Person/LastName">
    <LastName>
      <xsl:value-of select="." />
    </LastName>
  </xsl:template>

  <xsl:template match ="Person/CompanyInfo/CompanyName">
    <CompanyInfo>
      <CompanyName>
        <xsl:value-of select="." />
      </CompanyName>
    </CompanyInfo>
  </xsl:template>

  <xsl:template match="/">
    <xsl:if test="normalize-space(.) != '' or ./@* != ''">
      <xsl:copy>
        <xsl:copy-of select = "@*"/>
        <xsl:apply-templates/>
      </xsl:copy>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

然后我有这个示例XML:

<?xml version="1.0" encoding="utf-8"?>
<Information>
  <Person>
    <FirstName>Person 1</FirstName>
    <MiddleName/>
    <LastName>Last 1</LastName>
    <CompanyInfo>
      <CompanyName></CompanyName>
    </CompanyInfo>
  </Person>
  <Person>
    <FirstName>Person 2</FirstName>
    <MiddleName>Mid 2<MiddleName/>
    <LastName>Last 2</LastName>
    <CompanyInfo>
      <CompanyName>CompName 2</CompanyName>
    </CompanyInfo>
  </Person>
</Information>

等。

我需要这样的输出:

<Person>
  <FirstName>Person 1</FirstName>
  <LastName>Last 1</LastName>
</Person>
<Person>
  <FirstName>Person 2</FirstName>
  <MiddleName>Mid 2<MiddleName/>
  <LastName>Last 2</LastName>
  <CompanyInfo>
     <CompanyName>CompName 2</CompanyName>
  </CompanyInfo>
</Person>

但我认为我的xslt没有给我所需的输出,我需要的是处理每个特定的模板,然后仍然需要处理我在这里得到的最后一个身份模板,请任何帮助:)

如果内部的所有内容都是空的,我试图在这里做的是省略空的标签,但是我已经创建了几个模板来执行我想要输出的除了空标签之外

1 个答案:

答案 0 :(得分:0)

我将从身份转换模板开始,然后添加一个模板,防止复制您不想要的那些元素:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

<xsl:template match="*[not(node()[normalize-space()])]"/>

</xsl:stylesheet>

您需要添加

  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

获得一致的缩进。

这是一个完整的例子,当我应用代码

<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="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="*[not(node()[normalize-space()])]"/>

</xsl:stylesheet>

到更正后的输入

<?xml version="1.0" encoding="utf-8"?>
<Information>
  <Person>
    <FirstName>Person 1</FirstName>
    <MiddleName/>
    <LastName>Last 1</LastName>
    <CompanyInfo>
      <CompanyName></CompanyName>
    </CompanyInfo>
  </Person>
  <Person>
    <FirstName>Person 2</FirstName>
    <MiddleName>Mid 2</MiddleName>
    <LastName>Last 2</LastName>
    <CompanyInfo>
      <CompanyName>CompName 2</CompanyName>
    </CompanyInfo>
  </Person>
</Information>

然后我得到了结果

<Information>
   <Person>
      <FirstName>Person 1</FirstName>
      <LastName>Last 1</LastName>
   </Person>
   <Person>
      <FirstName>Person 2</FirstName>
      <MiddleName>Mid 2</MiddleName>
      <LastName>Last 2</LastName>
      <CompanyInfo>
         <CompanyName>CompName 2</CompanyName>
      </CompanyInfo>
   </Person>
</Information>

如果您确实不想输出根元素,请使用

<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="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="/*">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="*[not(node()[normalize-space()])]"/>

</xsl:stylesheet>

在这种情况下,您会得到一个包含多个Person元素的片段:

<Person>
   <FirstName>Person 1</FirstName>
   <LastName>Last 1</LastName>
</Person>
<Person>
   <FirstName>Person 2</FirstName>
   <MiddleName>Mid 2</MiddleName>
   <LastName>Last 2</LastName>
   <CompanyInfo>
      <CompanyName>CompName 2</CompanyName>
   </CompanyInfo>
</Person>