XSL从输入XML到输出XML的转换

时间:2014-08-20 10:36:46

标签: xml xslt input output transform

我有一个输入XML,需要使用XSLT转换为另一个XML结构。有人可以帮帮我吗。我尝试了解XSL教程,但这看起来并不复杂。

我需要将根节点转换为实体,并且存在具有属性类型的节点,此类节点的文本值需要映射到新的属性ID。此类节点的name属性也需要重命名为lookupAttr。非常感谢。

输入XML

 <xml-fragment type="salesorder">
  <customerid name="bppartnerid" type="account">120100</customerid>
  <freightamount>0.000</freightamount>
  <orderreference/>
  <signatureid name="signaturecode" type="signature">CHRHUG</signatureid>
  <termsofpaymentid name="termsofpaymentcode" type="termsofpayment">30</termsofpaymentid>
  <name>ordernumber#E1ORNO</name>
  <ordernumber>10440</ordernumber>
  <pricelevelid name="name" type="pricelevel">01 GBP</pricelevelid>
  <transactioncurrencyid name="isocurrencycode" type="transactioncurrency">GBP</transactioncurrencyid>
</xml-fragment>

OutputXML

<Entity type="salesorder">

  <customerid lookupAttr="bppartnerid" type="account" id= "120100"></customerid>
  <freightamount>0.000</freightamount>
  <orderreference/>
  <signatureid lookupAttr="signaturecode" type="signature" id="CHRHUG"/>
  <termsofpaymentid lookupAttr="termsofpaymentcode" type="termsofpayment" id="30"></termsofpaymentid>
  <ordernumber>10440</ordernumber>
  <pricelevelid lookupAttr="name" type="pricelevel" id="01 GBP"></pricelevelid>
  <transactioncurrencyid lookupAttr="isocurrencycode" type="transactioncurrency" id="GBP"></transactioncurrencyid>

</Entity>

1 个答案:

答案 0 :(得分:0)

在这里,你有一个例子可以帮助你上路;

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
    <xsl:template match="xml-fragment">
        <Entity>
            <xsl:attribute name="type">
                <xsl:value-of select="@type"/>
            </xsl:attribute>
            <customerid>
                <xsl:attribute name="lookupAttr">
                    <xsl:value-of select="customerid/@name"/>
                </xsl:attribute>
                <xsl:attribute name="type">
                    <xsl:value-of select="customerid/@type"/>
                </xsl:attribute>        
                <xsl:value-of select="customerid"/>
            </customerid>
            <freightamount>
                <xsl:value-of select="freightamount"/>
            </freightamount>
        </Entity>
    </xsl:template>
</xsl:stylesheet>