Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XLS按属性对xml节点排序_Xml_Xslt - Fatal编程技术网

XLS按属性对xml节点排序

XLS按属性对xml节点排序,xml,xslt,Xml,Xslt,我尝试按属性值对xml节点进行排序,但尽管我对它们进行了正确排序,但无法将相同的xml更改为输出 来源: XSL: 预期输出: 给定的输入XML: <?xml version="1.0" encoding="UTF-8"?> <enumeration attributeTypeId="com.ibm.team.workitem.enumeration.area" name="Área"> <literal externalValue="Enol" id

我尝试按属性值对xml节点进行排序,但尽管我对它们进行了正确排序,但无法将相同的xml更改为输出

来源:


XSL:


预期输出:


给定的输入XML:

<?xml version="1.0" encoding="UTF-8"?>
<enumeration attributeTypeId="com.ibm.team.workitem.enumeration.area" name="Área">
  <literal externalValue="Enol" id="com.ibm.team.workitem.enumeration.area.literal.l3" name="Enol"/>
  <literal externalValue="Pasivo" id="com.ibm.team.workitem.enumeration.area.literal.l8" name="Pasivo"/>
  <literal externalValue="Proyectos" id="com.ibm.team.workitem.enumeration.area.literal.l5" name="Proyectos"/>
  <literal externalValue="Servicios" id="com.ibm.team.workitem.enumeration.area.literal.l10" name="Servicios"/>
  <literal default="true" externalValue="---" id="---" name="---" null="true" />
  <literal externalValue="Activo" id="com.ibm.team.workitem.enumeration.area.literal.l7" name="Activo"/>
  <literal externalValue="Canales y MMP" id="com.ibm.team.workitem.enumeration.area.literal.l9" name="Canales y MMP"/>
  <literal externalValue="Arquitectura" id="com.ibm.team.workitem.enumeration.area.literal.l4" name="Arquitectura"/>
  <literal externalValue="Centro Información" id="com.ibm.team.workitem.enumeration.area.literal.l6" name="Centro Información"/>
  <literal externalValue="Desarrollo Corporativo" id="com.ibm.team.workitem.enumeration.area.literal.l2" name="Desarrollo Corporativo"/>
  <literal externalValue="Producción y Sistemas" id="com.ibm.team.workitem.enumeration.area.literal.l1" name="Producción y Sistemas"/>
</enumeration>

给定XSLT系统表:

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

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

<xsl:template match="enumeration">
  <xsl:copy>
    <xsl:apply-templates select="literal">
      <xsl:sort select="@name"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

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

</xsl:stylesheet>

产量:

<?xml version="1.0" encoding="UTF-8"?>
<enumeration>
  <literal default="true" externalValue="---" id="---" name="---" null="true"/>
  <literal externalValue="Activo" id="com.ibm.team.workitem.enumeration.area.literal.l7" name="Activo"/>
  <literal externalValue="Arquitectura" id="com.ibm.team.workitem.enumeration.area.literal.l4" name="Arquitectura"/>
  <literal externalValue="Canales y MMP" id="com.ibm.team.workitem.enumeration.area.literal.l9" name="Canales y MMP"/>
  <literal externalValue="Centro Información" id="com.ibm.team.workitem.enumeration.area.literal.l6" name="Centro Información"/>
  <literal externalValue="Desarrollo Corporativo" id="com.ibm.team.workitem.enumeration.area.literal.l2" name="Desarrollo Corporativo"/>
  <literal externalValue="Enol" id="com.ibm.team.workitem.enumeration.area.literal.l3" name="Enol"/>
  <literal externalValue="Pasivo" id="com.ibm.team.workitem.enumeration.area.literal.l8" name="Pasivo"/>
  <literal externalValue="Producción y Sistemas" id="com.ibm.team.workitem.enumeration.area.literal.l1" name="Producción y Sistemas"/>
  <literal externalValue="Proyectos" id="com.ibm.team.workitem.enumeration.area.literal.l5" name="Proyectos"/>
  <literal externalValue="Servicios" id="com.ibm.team.workitem.enumeration.area.literal.l10" name="Servicios"/>
</enumeration>


我不完全确定是否可以在for-each循环中按属性值排序。另外,您应该避免使用for each循环,而是使用模板。

实现这一点的一种可能性是以下样式表。根据需要调整模板
match=

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

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes" method="xml" />
  <xsl:template match="/enumeration[@attributeTypeId='com.ibm.team.workitem.enumeration.area' and @name='Área']">
    <enumeration attributeTypeId="{@attributeTypeId}" name="{@name}">
      <xsl:for-each select="literal">
        <xsl:sort select="@name"/>
        <xsl:copy-of select="."/>
      </xsl:for-each>
    </enumeration>
  </xsl:template>
</xsl:stylesheet>

这导致

<?xml version="1.0"?>
<enumeration attributeTypeId="com.ibm.team.workitem.enumeration.area" name="&#xC1;rea">
  <literal default="true" externalValue="---" id="---" name="---" null="true"/>
  <literal externalValue="Activo" id="com.ibm.team.workitem.enumeration.area.literal.l7" name="Activo"/>
  <literal externalValue="Arquitectura" id="com.ibm.team.workitem.enumeration.area.literal.l4" name="Arquitectura"/>
  <literal externalValue="Canales y MMP" id="com.ibm.team.workitem.enumeration.area.literal.l9" name="Canales y MMP"/>
  <literal externalValue="Centro Informaci&#xF3;n" id="com.ibm.team.workitem.enumeration.area.literal.l6" name="Centro Informaci&#xF3;n"/>
  <literal externalValue="Desarrollo Corporativo" id="com.ibm.team.workitem.enumeration.area.literal.l2" name="Desarrollo Corporativo"/>
  <literal externalValue="Enol" id="com.ibm.team.workitem.enumeration.area.literal.l3" name="Enol"/>
  <literal externalValue="Pasivo" id="com.ibm.team.workitem.enumeration.area.literal.l8" name="Pasivo"/>
  <literal externalValue="Producci&#xF3;n y Sistemas" id="com.ibm.team.workitem.enumeration.area.literal.l1" name="Producci&#xF3;n y Sistemas"/>
  <literal externalValue="Proyectos" id="com.ibm.team.workitem.enumeration.area.literal.l5" name="Proyectos"/>
  <literal externalValue="Servicios" id="com.ibm.team.workitem.enumeration.area.literal.l10" name="Servicios"/>
</enumeration>

而不是:

<xsl:template match="/">
      <xsl:for-each select="enumeration/literal">
        <xsl:sort select="@name"/>
        <xsl:value-of select="@name"/>
        <!-- this should be different, I tried with select="current()" too -->
      </xsl:for-each>
</xsl:template>

尝试:



您的第一个模板是多余的。@michael.hor257k您是对的,但我总是把它作为一种习惯性的力量保留下来。
<xsl:template match="/enumeration">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:for-each select="literal">
            <xsl:sort select="@name"/>
            <xsl:copy-of select="."/>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>