使用基于子节点的XSLT过滤XML

时间:2015-07-24 00:03:58

标签: xml xslt logic xslt-1.0

我有下面的XML,我想使用XSLT过滤XML以基于IsBusiness分离CompanyType

   <CompanyTypes>
            <CompanyType>
               <CompanyTypeID>1</CompanyTypeID>
               <CompanyTypeName>Individual</CompanyTypeName>
               <CompanyTypeDesc>Individual</CompanyTypeDesc>
               <IsBusiness>N</IsBusiness>
            </CompanyType>
            <CompanyType>
               <CompanyTypeID>2</CompanyTypeID>
               <CompanyTypeName>Proprietorship</CompanyTypeName>
               <CompanyTypeDesc>Proprietorship</CompanyTypeDesc>
               <IsBusiness>N</IsBusiness>
            </CompanyType>
            <CompanyType>
               <CompanyTypeID>3</CompanyTypeID>
               <CompanyTypeName>Trust Irrevocable</CompanyTypeName>
               <CompanyTypeDesc>Irrevocable Trust</CompanyTypeDesc>
               <IsBusiness>N</IsBusiness>
            </CompanyType>
            <CompanyType>
               <CompanyTypeID>4</CompanyTypeID>
               <CompanyTypeName>Trust Revocable</CompanyTypeName>
               <CompanyTypeDesc>Revocable Trust</CompanyTypeDesc>
               <IsBusiness>N</IsBusiness>
            </CompanyType>
            <CompanyType>
               <CompanyTypeID>5</CompanyTypeID>
               <CompanyTypeName>Association</CompanyTypeName>
               <CompanyTypeDesc>Association</CompanyTypeDesc>
               <IsBusiness>Y</IsBusiness>
            </CompanyType>
          </CompanyTypes>

我想在

中获得所有IsBusiness = N.
<CompanyTypesIndividual>
  <CompanyType>
   <CompanyTypeID>3</CompanyTypeID>
   <CompanyTypeName>Trust Irrevocable</CompanyTypeName>
   <CompanyTypeDesc>Irrevocable Trust</CompanyTypeDesc>
   <IsBusiness>N</IsBusiness>
  </CompanyType>
 <CompanyType>
   <CompanyTypeID>4</CompanyTypeID>
   <CompanyTypeName>Trust Revocable</CompanyTypeName>
   <CompanyTypeDesc>Revocable Trust</CompanyTypeDesc>
   <IsBusiness>N</IsBusiness>
  </CompanyType>
</CompanyTypesIndividual>

和ISBusiness = Y

<CompanyTypesBusiness>
  <CompanyType>
   <CompanyTypeID>3</CompanyTypeID>
   <CompanyTypeName>Trust Irrevocable</CompanyTypeName>
   <CompanyTypeDesc>Irrevocable Trust</CompanyTypeDesc>
   <IsBusiness>Y</IsBusiness>
  </CompanyType>
<CompanyType>
   <CompanyTypeID>3</CompanyTypeID>
   <CompanyTypeName>Trust Irrevocable</CompanyTypeName>
   <CompanyTypeDesc>Irrevocable Trust</CompanyTypeDesc>
   <IsBusiness>Y</IsBusiness>
  </CompanyType>
</CompanyTypesBusiness>

非常感谢您的反馈

2 个答案:

答案 0 :(得分:1)

我无法将您的输入与预期输出相关联。 使用IsBusiness ='N'获取CompanyType的xpath将是:

/CompanyTypes/CompanyType[IsBusiness = 'N']

答案 1 :(得分:0)

以下XSLT

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 <xsl:output method="xml"  omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
 <xsl:strip-space elements="*"/>
  <xsl:template match="/">
    <CompanyTypesIndividual>
      <xsl:apply-templates select="CompanyTypes/CompanyType[IsBusiness='N']"/>
    </CompanyTypesIndividual>
    <CompanyTypesBusiness>
      <xsl:apply-templates select="CompanyTypes/CompanyType[IsBusiness='Y']"/>
    </CompanyTypesBusiness>
  </xsl:template>
  <xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:transform>

当应用于您的输入时,XML会产生所需的输出 与XML根匹配的模板将模板应用于CompanyTypes包含在IsBusiness='N'组中的所有<CompanyTypesIndividual>,然后应用于CompanyTypes包含在IsBusiness='Y'中的所有<CompanyTypesBusiness> 1}}组。应用的<xsl:template match="@*|node()">只复制节点和属性 为方便起见,我保存了转化here

更新:正如michael.hor257k所评论的那样,此结果不是有效的XML。要拥有有效的XML,结果文件必须以相同的元素开始和结束,例如通过这种调整:

<xsl:template match="/">
    <CompanyTypes>
      <CompanyTypesIndividual>
        <xsl:apply-templates select="CompanyTypes/CompanyType[IsBusiness='N']"/>
      </CompanyTypesIndividual>
      <CompanyTypesBusiness>
        <xsl:apply-templates select="CompanyTypes/CompanyType[IsBusiness='Y']"/>
      </CompanyTypesBusiness>
    <CompanyTypes>
</xsl:template>  

此外,您可能希望删除omit-xml-declaration="yes"或将其更改为omit-xml-declaration="no"以将XML声明保留在输出XML中。