java - 通过 Java 应用 XSLT 进行通用 XML 排序?

标签 java xml xslt

基本上,我想要 XML 的精确副本,但我想按某些节点的 ID 属性对其进行排序。并非所有具有 ID 属性的元素都应该排序。

我已经拼凑了一个工作样式表,但它需要我对我正在排序的节点的所有同级节点的 进行硬编码。我没有正式的模式,我不能确定它不会改变。我希望能够创建一个更通用的样式表,它将输出未排序的节点,而无需这些显式调用。

这可能吗?

我的用于转换 XML 的 Java 代码:

String input = "C:\\presort.xml";
String output = "C:\\postsort.xml";
String xsl = "C:\\sort.xsl"
Document dom;

try
{
 dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(input);
 Element e = dom.getDocumentElement();

 Transformer transform = TransformerFactory.newInstance().newTransformer(
   new StreamSource( new File(xsl) ));
 StreamResult result = new StreamResult(new File(output));
 transform.transform(new DOMSource(dom), result);
} 
catch (Exception e)
{
 e.printStackTrace();
}    

我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <Person id="1">
    <Base>true</Base>
    <Description>something</Description>
    <Name>Vin Vinigar</Name>
    <Privileges>
      <Privilige id="340">
        <Permission>2</Permission>
        <Job id="dontsort" />
      </Privilige>
      <Privilige id="11">
        <Permission>3</Permission>
        <Job id="dontsort" />
      </Privilige>
      <Privilige id="1011">
        <Permission>1</Permission>
        <Job id="2342" />
      </Privilige>
    </Privileges>
  </Person>

  <Person id="f32">
    <Base>true</Base>
    <Description>Here be dragons</Description>
    <Name>John Doe</Name>
    <Privileges>
      <Privilige id="23a">
        <Permission>2</Permission>
        <Job id="a2a" />
      </Privilige>
    </Privileges>
  </Person>

  <Person id="22">
    <PossibleUnknownTagHere>something</PossibleUnknownTagHere>
    <Name>Han Solo</Name>
    <Privileges>
      <Privilige id="23a">
        <Permission>3</Permission>
        <Job id="a2a" />
      </Privilige>
    </Privileges>
  </Person>

</root>

我的样式表:

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

    <xsl:template match="root|@*">
        <xsl:copy >
            <xsl:apply-templates select="@*" />
            <xsl:apply-templates select="Person">
                <xsl:sort select="@id" order="ascending" />
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Person">
        <xsl:copy >
            <xsl:apply-templates select="@*" />

            <!-- Works but requies a template for each sibling element -->
            <xsl:apply-templates select="Base" />
            <xsl:apply-templates select="Description" />
            <xsl:apply-templates select="Name" />

            <!-- I'd like to do something like this -->
            <!--
            <xsl:choose>
                <xsl:when test="not(Privileges)">
                    <xsl:apply-templates select="." />
                </xsl:when>
            </xsl:choose>
            -->

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

    <!-- I'd like to remove the need for these 3 explicity copies -->
    <xsl:template match="Base">
        <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="Description">
        <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="Name">
        <xsl:copy-of select="."/>
    </xsl:template>

    <xsl:template match="Privileges">
        <xsl:copy >
            <xsl:apply-templates select="@*" />
            <xsl:apply-templates select="Privilege">
                <xsl:sort select="@id" order="ascending" />
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Privilege">
        <xsl:copy-of select="."/>
    </xsl:template>

</xsl:stylesheet>

最佳答案

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

<xsl:template match="root">
    <xsl:copy>
        <xsl:apply-templates select="@*" />
        <xsl:apply-templates select="Person">
            <xsl:sort select="@id" order="ascending" />
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

<xsl:template match="Privileges">
    <xsl:copy>
        <xsl:apply-templates select="@*" />
        <xsl:apply-templates select="Privilege">
            <xsl:sort select="@id" order="ascending" />
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

关于java - 通过 Java 应用 XSLT 进行通用 XML 排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1447254/

相关文章:

java - 运行旧 Java 小程序的最直接方式

java - OpenGL ES 2.0 中的立方体不绘制给定的纹理

java - java如何解析XML文件

c++ - XSLT 到 HTML 的转换。 Qt5中的generate-id()错误

xslt - 使用 XSLT 向标签添加属性

java - 请求参数和 PUT 方法

java - 如何根据方向旋转标记(总线)?

xml - eclipse 3.5 : How to get file name from Editor?

python - 使用 SQL 查询更新 XML

java - XSLT 收集数据