通过XSLT设置XML属性的顺序?

时间:2016-08-30 12:51:00

标签: java html xml xslt

我使用XML格式的HTML,我正在使用XSLT进行解析。我的HTML看起来像这样:

<html>
<head>
<meta charset="utf-8" />
<title>Test</title>
</head>
<body>
<img height=""  width="' src="google.gif?<>" />
</body>
</html>

在XSLT解析之后,它看起来像这样:

 <html>
    <head>
    <meta charset="utf-8" />
    <title>Test</title>
    </head>
    <body>
    <img height="" src="google.gif?<>" width=""/>
    </body>
    </html>

我希望@src作为<img height="" width="" src="google.gif?<>" />之类的最后一个属性,但默认情况下,属性按字母顺序排序。我无法使用<xsl:sort>

3 个答案:

答案 0 :(得分:1)

XSLT生成符合XDM数据模型的结果树作为输出,并且在XDM模型中,属性是无序的。由于它们没有订单,因此XSLT样式表指令无法控制订单。

在序列化过程中,当结果树中的无序属性节点在词法XML输出中转换为name =“value”对的有序序列时,控制顺序的唯一机会就出现了。 XSLT(任何版本)中提供的标准序列化属性不提供任何控制方法。然而,Saxon有一个扩展属性saxon:attribute-order - 请参阅

http://www.saxonica.com/documentation/index.html#!extensions/output-extras/serialization-parameters

答案 1 :(得分:0)

输入HTML (包含信息):

<html>
<head>
    <meta charset="utf-8" />
    <title>Test</title>
</head>
    <body>
        <img height="13" width="12" src="google.gif?" id="id1"/>
    </body>
</html>

<强> XSLT:

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

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

<xsl:template match="img">
    <xsl:copy>
        <xsl:for-each select="@*[not(name()='src')]">
            <xsl:sort select="name()"/>
            <xsl:attribute name="{name()}">
                <xsl:value-of select="."/>
            </xsl:attribute>
        </xsl:for-each>
        <xsl:apply-templates select="@*[name()='src']"/>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

<强>结果:

<html>
<head>
  <meta charset="utf-8"/>
  <title>Test</title>
</head>
<body>
  <img height="13" id="id1" width="12" src="google.gif?"/>
</body>
</html>

答案 2 :(得分:0)

除了class WC_Expedited_Order_Email extends WC_Email { public function __construct() { $this->id = 'expedited_order_tracking'; $this->customer_email = true; $this->title = __( 'Shippement Traking', 'customEmail' ); $this->description = __( 'Sent tracking email to customer', 'customEmail' ); $this->heading = __( 'Your {site_title} order is shipped', 'customEmail' ); $this->subject = __( 'Your {site_title} order from {order_date} is shipped', 'customEmail' ); $this->template_html = 'emails/customer-order_tracking.php'; $this->template_plain = 'emails/plain/customer-order_tracking.php'; add_action( 'woocommerce_order_status_shipped', array( $this, 'trigger' ) ); parent::__construct(); } public function trigger( $order_id ) { var_dump($order_id);die(); } 格式不正确commented by Martin Honnen ...

之外 根据{{​​3}}

Attribute order is insignificant

  

请注意start-tag或中的属性规范的顺序   空元素标签不重要。

因此,XSLT无法约束属性排序。

如果您拒绝接受此建议以忽略属性排序,请参阅XML Recommendation上之前的Martin Honnen's suggestions regarding how to control attribute ordering output问题。