0

我有以下 XML

<?xml version="1.0" encoding="utf-8" ?> 
<listGroup name="Chemistry Lists">
  <chemistryAnalysisMethod tableName="List Chemistry Analysis Method">
    <option>
      <Value>BTEX</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>Carbonates</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>Chlorinated Hydrocarbons</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>Explosives</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>Halogenated Hydrocarbons</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>Heavy Metals</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>Herbicides</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>Metals</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>OCP</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>Organophosphorus Pesticides</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>Organotins</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>PAH</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>PAH/Phenols</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>PCB</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>Pesticides</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>Phenols</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>Phthalates</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>SVOC</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>TPH</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>VOC</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>Dissolved Mercury by FIMS</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>Dissolved Metals by ICPMS</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>Nitrite and Nitrate</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
    <option>
      <Value>OE - Wet</Value>
      <ContainerType />
      <RequiredVolume />
      <Preservative />
      <HoldTime />
      <NumberofJars />
      <Filtered>false</Filtered>
    </option>
  </chemistryAnalysisMethod> 
</listGroup>

我想按Value元素排序,我编写了以下 XSLT

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        exclude-result-prefixes="xsl">   
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />   
  <xsl:strip-space elements="*" />   

  <xsl:template match="/serverInterface">
    <interface>
      <xsl:attribute name="firstFormAPIName">
    <xsl:value-of select="@firstFormAPIName"/>
      </xsl:attribute>

      <xsl:for-each select="listGroup/*">
    <xsl:sort select="@tableName" />
    <xsl:element name="list">
      <xsl:attribute name="tableName">
        <xsl:value-of select="@tableName" />
      </xsl:attribute>       
    </xsl:element>
      </xsl:for-each>

      <xsl:for-each select="form">
    <xsl:if test="not(@enabled) or @enabled='always' or @enabled='yes'">
      <xsl:element name="form">
        <xsl:attribute name="apiName">
          <xsl:value-of select="@apiName" />
        </xsl:attribute>
        <xsl:attribute name="title">
          <xsl:value-of select="@title" />
        </xsl:attribute>
        <xsl:attribute name="template">
          <xsl:value-of select="@template" />
        </xsl:attribute>
        <xsl:attribute name="fieldDataTableName">
          <xsl:value-of select="@fieldDataTableName" />
        </xsl:attribute>
        <xsl:if test="not(@apiName='projectForm')">
          <xsl:attribute name="parentFieldDataTableName">
        <xsl:value-of select="@parentFieldDataTableName" />
          </xsl:attribute>
        </xsl:if>
        <xsl:apply-templates select="*[not(name()='group') and not(name()='serverInterface') and not(name()='list')]" />
      </xsl:element>
    </xsl:if>

    <xsl:for-each select="group">
      <xsl:if test="not(@enabled) or @enabled='always' or @enabled='yes'">
        <xsl:element name="form">
          <xsl:attribute name="apiName">
        <xsl:value-of select="@apiName" />
          </xsl:attribute>
          <xsl:attribute name="title">
        <xsl:value-of select="@title" />
          </xsl:attribute>
          <xsl:attribute name="template">
        <xsl:value-of select="@template" />
          </xsl:attribute>
          <xsl:attribute name="fieldDataTableName">
        <xsl:value-of select="../@fieldDataTableName" />
          </xsl:attribute>
          <xsl:attribute name="parentFieldDataTableName">
        <xsl:value-of select="../@parentFieldDataTableName" />
          </xsl:attribute>
          <xsl:apply-templates select="*[not(name()='group') and not(name()='serverInterface') and not(name()='list')]" />
        </xsl:element>
      </xsl:if>
    </xsl:for-each>
      </xsl:for-each>
    </interface>
    <xsl:apply-templates>
    </xsl:apply-templates>   
  </xsl:template>

  <xsl:template match ="listGroup/chemistryAnalysisMethod">
    <xsl:apply-templates>
      <xsl:sort select="Value"/>
    </xsl:apply-templates>   
  </xsl:template>

  <xsl:template match="option">
    <xsl:value-of select="Value"/>   
  </xsl:template>

  <xsl:template match="*[not(name()='group') and not(name()='serverInterface') and not(name()='list')]">
    <xsl:if test="not(@enabled) or @enabled='always' or @enabled='yes'">
      <xsl:element name="{name()}">
    <xsl:choose>
      <xsl:when test="name()='shortcut'">
      </xsl:when>
      <xsl:when test="name()='label'">
      </xsl:when>
      <xsl:when test="name()='blowCount'">
      </xsl:when>
      <xsl:when test="name()='photo'">
      </xsl:when>
      <xsl:when test="name()='color'">
      </xsl:when>
      <xsl:when test="name()='munsellColor'">
      </xsl:when>
      <xsl:when test="name()='advancementMethod'">
      </xsl:when>
      <xsl:when test="name()='button'">
      </xsl:when>
      <xsl:when test="name()='constituentPercentages'">
      </xsl:when>
      <xsl:when test="name()='orientation'">
      </xsl:when>
      <xsl:when test="name()='gpsControl'">
      </xsl:when>
      <xsl:when test="name()='constituents'">
      </xsl:when>
      <xsl:when test="name()='rockType'">
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="@requiredValue='always' or @requiredValue='yes'">
        <xsl:attribute name="requiredValue">true</xsl:attribute>
        <xsl:attribute name="apiName">
          <xsl:value-of select="@apiName" />
        </xsl:attribute>
          </xsl:when>
          <xsl:otherwise>
        <xsl:attribute name="requiredValue">false</xsl:attribute>
        <xsl:attribute name="apiName">
          <xsl:value-of select="@apiName" />
        </xsl:attribute>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:for-each select="@*[not(name()='requiredValue' or name()='enabled' or name()='moveable')]">
      <xsl:copy-of select="." />
    </xsl:for-each>
    <xsl:copy-of select="*" />
      </xsl:element>
    </xsl:if>   
  </xsl:template>

</xsl:stylesheet>

当我试图这样做时,没有完成任何转换。

4

1 回答 1

0

如果您只想输出相同的 XML,但元素option按 排序Value,那么以下 XSLT 样式表将执行此操作:

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

  <!-- This is a modified identity transform. -->
  <xsl:template match="@* | node()">
    <xsl:copy>
      <!-- Simply copy all attributes and child nodes, except for option nodes. -->
      <xsl:apply-templates select="@* | node()[not(self::option)]"/>
      <!-- Copy out all child option nodes, but order them by Value. -->
      <xsl:apply-templates select="option">
        <xsl:sort select="Value"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

但是,您似乎正在尝试在示例 XSLT 中进行其他转换。如果没有您想要实现的 XML 示例,我无法确定它们是什么。

于 2013-09-06T16:30:59.840 回答