使用XSLT向XML添加属性和值

时间:2017-01-18 08:51:56

标签: xml xslt xslt-2.0

我的输入XML如下:

<?xml version="1.0" encoding="UTF-8"?>
<PQ>
   <PQGetClaimAttachment>
      <USERID>XXX</USERID>
      <PASSWORD>XXX</PASSWORD>
      <hostAddress>XXX</hostAddress>
      <businessArea>CS2XAA</businessArea>
      <sourceType>2XSRCTYP</sourceType>
      <operator>GT</operator>
      <lookupDate>2015-01-01T00.00.00.000000+05:30</lookupDate>
   </PQGetClaimAttachment>
</PQ>

我想使用XSLT将上面的xml转换为下面的xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<lookupInstances xmlns="http://www.tech.com/awd/rest/v1" name="XYZ">
  <lookupParameters>
    <lookupParameter name="businessArea">CS2XAA</lookupParameter>
    <lookupParameter name="type">2XSRCTYP</lookupParameter>
    <lookupParameter name="OPER">GT</lookupParameter>
    <lookupParameter name="LDAT">2015-01-01T00.00.00.000000+05:30</lookupParameter>
  </lookupParameters>
</lookupInstances>

我正在使用以下XSLT,但它没有给我所需的输出XML。

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

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

    <xsl:template match="/">
            <lookupInstances xmlns="http://www.tech.com/awd/rest/v1"
                name="XYZ">
                <lookupParameters>
                    <lookupParameter businessArea="{//businessArea}" />
                    <lookupParameter type="{//sourceType}" />
                    <lookupParameter OPER="{//operator}" />
                    <lookupParameter LDAT="{//lookupDate}" />
                </lookupParameters>
            </lookupInstances>
    </xsl:template>

</xsl:stylesheet>

任何人都可以让我知道我在做错的地方吗?

1 个答案:

答案 0 :(得分:1)

如果您希望输出具有name="businessArea"属性,则为它们提供businessArea=""属性是没有意义的。

这应该做你需要的:

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

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

    <xsl:template match="/">
            <lookupInstances xmlns="http://www.tech.com/awd/rest/v1"
                name="XYZ">
                <lookupParameters>
                    <lookupParameter name="businessArea">
                         <xsl:value-of select="PQ/PQGetClaimAttachment/businessArea" />
                    </lookupParameter>
                    <lookupParameter name="type">
                         <xsl:value-of select="PQ/PQGetClaimAttachment/sourceType" />
                    </lookupParameter>
                    <lookupParameter name="OPER">
                         <xsl:value-of select="PQ/PQGetClaimAttachment/operator" />
                    </lookupParameter>
                    <lookupParameter name="LDAT">
                         <xsl:value-of select="PQ/PQGetClaimAttachment/lookupDate" />
                    </lookupParameter>
                </lookupParameters>
            </lookupInstances>
    </xsl:template>

</xsl:stylesheet>