繁体   English   中英

使用XSLT在任何级别进行元素匹配

[英]Match On Element At Any Level Using XSLT

当匹配节点是父节点的子节点时,下面的XSLT可以按照预期的方式工作。 AttributeID中的值用于替换元素名称,原始元素名称保存在名为OrgName的新属性中。

<?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:strip-space elements="*"/>

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

  <!-- Change Values/Value value of AttributeID -->
  <xsl:template match="Values/Value|MultiValue|MetaData/Value">
    <xsl:element level="any" name="{@AttributeID}">
    <xsl:attribute name="OrgName">
      <xsl:value-of select="name()" />
    </xsl:attribute>      
        <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>

  <!--empty template suppresses this attribute-->
  <xsl:template match="@AttributeID" />

</xsl:stylesheet>

但是,如果匹配节点嵌套在多个级别(以及各个级别),是否有一种方法可以在任何地方进行匹配而无需特别指定路径? 我尝试了level =“ any”以及更改匹配项以包括根父项,但没有成功。

  <xsl:template match="Values/Value|MultiValue|RootParent//MetaData/Value">

任何帮助表示赞赏。 示例数据如下:这是原始文件的精简版本,非常大。 我在XSLT应该更改的行上添加了注释。

<?xml version="1.0" encoding="UTF-8"?>
<ROOT-ProductInformation ExportTime="2017-10-02 16:24:31" ExportContext="Context1" ContextID="Context1" WorkspaceID="Main" UseContextLocale="false">
  <Classifications>
    <Classification ID="Classification 1 root" UserTypeID="Classification 1 user-type root" Selected="false">
      <Name>Classification 1 root</Name>
      <AttributeLink AttributeID="user.documentvalue" />  <!-- NOT Here -->
      <Classification ID="Application_Root_Hierarchy" UserTypeID="Application_Root" Selected="false">
        <Name>Application Hierarchy</Name>
        <Classification ID="Application_Folder" UserTypeID="Application_Folder" Selected="false">
          <Name>Applications</Name>
          <Classification ID="W" UserTypeID="Application" Selected="false" Referenced="true">
            <Name>W</Name>
            <MetaData>
              <Value AttributeID="APPLICATIONGROUP">W</Value>  <!-- Here -->
            </MetaData>
            <Classification ID="pt213996" UserTypeID="Protocol" Selected="false" Referenced="true">
              <Name>Western</Name>
                <EntityCrossReference EntityID="s214146" Type="Protocol_To_Species" />
              <MetaData>
                <Value AttributeID="PROTOCOLNO">19</Value>  <!-- Here -->
                <Value AttributeID="PROTOCOLDESCRIPTION">Western </Value>  <!-- Here -->
                <MultiValue AttributeID="PROTOCOLCOMPANIONITEMS">  <!-- Here -->
                  <Value>7720S</Value>
                  <Value>7722S</Value>
                </MultiValue>
                <Value AttributeID="APPLICATIONABBR">W</Value>  <!-- Here -->
              </MetaData>
            </Classification>
          </Classification>
        </Classification>
      </Classification>
    </Classification>
  </Classifications>
  <Entities>
    <Entity ID="t211773" UserTypeID="Target" ParentID="Target_Category" Selected="false" Referenced="true">
      <Name>Akt1</Name>
      <Values>
        <Value AttributeID="TARGETNO">T07427</Value>  <!-- Here -->
        <Value AttributeID="ACCESSION_GP">NP_005154</Value>  <!-- Here -->
        <MultiValue AttributeID="DEVSUBSECTIONS">  <!-- Here -->
          <Value>Something</Value>
        </MultiValue>
        <Value AttributeID="ORGANISM_COMMON_NAME">human</Value>  <!-- Here -->
      </Values>
    </Entity>
    <Entity ID="s214155" UserTypeID="Species" ParentID="Species_Root" Selected="false" Referenced="true">
      <Name>Hm</Name>
      <Values>
        <Value AttributeID="HOSTSPECIES_YN" ID="N">N</Value>  <!-- Here -->
      </Values>
    </Entity>
  </Entities>
  <Products>
    <Product ID="p721653" UserTypeID="Product" ParentID="Primary_Antibodies">
      <Name>DYKDDDDK</Name>
      <ProductCrossReference ProductID="KIT781787" Type="Product_To_KIT" />

      <EntityCrossReference EntityID="t353809" Type="Product_to_Target" />

      <Values>
        <Value AttributeID="PROJECTNO">P40841</Value>  <!-- Here -->
        <MultiValue AttributeID="TRADEMARKSTATEMENT">  <!-- Here -->
          <Value ID="SimpleABC">SimpleABC is a registered trademark</Value>
        </MultiValue>
        <Value AttributeID="DISCONTINUATIONREASON">PROD_PENDING_DISCONTINUE_REASON</Value>  <!-- Here -->
      </Values>
      <Product ID="ms750355" UserTypeID="MasterStock">
        <Name>14793MC</Name>
        <Values>
          <Value AttributeID="EBSExportFlag" ID="Y">Y</Value>  <!-- Here -->
        </Values>
        <Product ID="KIT781789" UserTypeID="KIT">
          <Name>14793P</Name>
          <Values>
            <Value AttributeID="STORAGEOPT">BLOCKS</Value>  <!-- Here -->
          </Values>
        </Product>
      </Product>
      <Product ID="ms750356" UserTypeID="MasterStock">
        <Name>14793MF</Name>
        <Values>
          <Value AttributeID="EBSExportFlag" ID="N">N</Value>  <!-- Here -->
        </Values>
        <Product ID="KIT781787" UserTypeID="KIT">
          <Name>14793BF</Name>
          <Values>
            <Value AttributeID="STORAGEOPT">SELF</Value>  <!-- Here -->
          </Values>
        </Product>
      </Product>
      <Product ID="pr801547" UserTypeID="Product_Revision">
        <Name>14793_rev1</Name>
          <ClassificationReference ClassificationID="pt213980" Type="Revision_to_ApplicationProtocol">
            <MetaData>
                            <MultiValue AttributeID="Appl_Species_Tested">  <!-- Here -->
                                <Value>All</Value>
                            </MultiValue>
                            <Value AttributeID="DILUTIONFACTOR">50</Value>  <!-- Here -->
                        </MetaData>
                    </ClassificationReference>
          <ProductCrossReference ProductID="ms750355" Type="ProductRevision_To_MasterStock" />
          <AssetCrossReference AssetID="14793W_0" Type="Published_Product_Images" />
        <Values>
          <Value AttributeID="RecombinantFlag_YN" ID="Y">Y</Value>  <!-- Here -->
        </Values>
      </Product>
    </Product>
  </Products>
</ROOT-ProductInformation>

这是一种转换具有该属性的元素ValueMultiValue的不同方法:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

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

    <!-- Change Value and MultiValue with @AttributeID -->
    <xsl:template match="Value[@AttributeID] | MultiValue[@AttributeID]">
      <xsl:element name="{@AttributeID}">
        <xsl:attribute name="OrgName">
          <xsl:value-of select="name()" />
        </xsl:attribute>      
        <xsl:apply-templates select="@*|node()"/>
      </xsl:element>        
    </xsl:template>

  <!--empty template suppresses this attribute-->
  <xsl:template match="Value/@AttributeID | MultiValue/@AttributeID" />

</xsl:transform>

http://xsltransform.net/bEzjRKN/1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM