xsl:sort 对缩进的副作用

Side-effect of xsl:sort on indentation

我需要根据属性 billOfMaterialItemID 的值对元素 BillOfMaterialIem 进行排序 示例:

<?xml version="1.0" encoding="UTF-8"?>
<PackageData documentCreateDate="2019-10-03" documentModificationDate="2019-10-03">
  <Items>
    <Item itemID="416664">
      <BillOfMaterial>
        <BillOfMaterialItem billOfMaterialItemID="417230" />
        <BillOfMaterialItem billOfMaterialItemID="417231" />
        <BillOfMaterialItem billOfMaterialItemID="416664-01"/>
        <BillOfMaterialItem billOfMaterialItemID="110424" />
      </BillOfMaterial>
    </Item>
  </Items>
</PackageData>

可以复制所有内容并过滤空属性:

<xsl:template match="node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()">
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>
<xsl:template match="@*">
  <xsl:if test="string-length(.)!=0">
    <xsl:copy-of select="."/>
  </xsl:if>
</xsl:template>

这是特定于元素 BillOfMaterial 的模板:

<xsl:template match="BillOfMaterial">
  <xsl:copy>
    <xsl:apply-templates select="BillOfMaterialItem" >
      <xsl:sort select="@billOfMaterialItemID"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

元素按预期排序但标识在输出中被删除 - 尽管缩进="yes"。 我不明白这种副作用的原因。

我错过了什么?

<xsl:strip-space elements="*"/> makes it work

谢谢你,michael.hor257k