XSLT更改属性值和排序

时间:2015-05-27 14:39:19

标签: xml xslt xml-parsing

我是XSLT的新手,我正试图找到一种方法来执行以下操作......

我有以下XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<AddCustomerRequest APIVersion="2.0" Name="Add Customers List Data">
    <CFParameters>
        <ReqId>1-YNU68</ReqId>
        <CfgId>1-TSP13</CfgId>
    </CFParameters>
    <Parameters>
        <ParName>Test Customer List</ParName>
        <ListName>Test Customer List</ListName>
        <Summary>N</Summary>
        <CustomerList>
            <CustomerInfo>
                <LoadID>1-YNU4R</LoadID>
                <CustomerID>1-E0FB</CustomerID>
                <LoadConID>1-YTQJ5</LoadConID>
                <TimeZone></TimeZone>
                <Available>Y</Available>
                <Phones>
                    <Phone PhoneType="2">6987991657</Phone>
                    <Phone PhoneType="1">6987991152</Phone>
                    <Phone PhoneType="4">6987999912</Phone>
                    <Phone PhoneType="3">6987999278</Phone>
                </Phones>
                <CustomFields></CustomFields>
            </CustomerInfo>
        </CustomerList>
    </Parameters>
</AddCustomerRequest>

每个客户(CustomerInfo元素)都有许多不同PhoneType的电话...... PhoneType值等效于以下内容:                 PhoneType“1”=商业                 PhoneType“2”=主页                 PhoneType“3”=其他                 PhoneType“4”=移动

请求是按以下PhoneType对电话进行排序: 1.移动 2.回家 3.业务 4.其他

因此,最终的XML应按以下顺序包含电话:

        <Phones>
                        <Phone PhoneType="4">6987999912</Phone>
                        <Phone PhoneType="2">6987991657</Phone>
                        <Phone PhoneType="1">6987991152</Phone>
                        <Phone PhoneType="3">6987999278</Phone>
        </Phones>

为了做到这一点,我相信我应该做以下事情(使用XSLT): •将PhoneType值替换为Temp值,以便能够排序 •按PhoneType对电话进行排序 •将临时PhoneType值替换为原始值

例如:

            Where PhoneType = “4” to be replaced by value “101”
            Where PhoneType = “2” to be replaced by value “102”
            Where PhoneType = “1” to be replaced by value “103”
            Where PhoneType = “3” to be replaced by value “104”

因此,原始XML的电话将是这样的:

<Phones>
<Phone PhoneType="102">6987991657</Phone>
<Phone PhoneType="103">6987991152</Phone>
<Phone PhoneType="101">6987999912</Phone>
<Phone PhoneType="104">6987999278</Phone>
</Phones>

然后,按PhoneType对电话进行排序,以便制作如下列表:

<Phones>
<Phone PhoneType="101">6987999912</Phone>
<Phone PhoneType="102">6987991657</Phone>
<Phone PhoneType="103">6987991152</Phone>
<Phone PhoneType="104">6987999278</Phone>
</Phones>

最后,用原始值替换临时PhoneType值:

            Where PhoneType = “101” to be replaced by value “4”
            Where PhoneType = “102” to be replaced by value “2”
            Where PhoneType = “103” to be replaced by value “1”
            Where PhoneType = “104” to be replaced by value “3”

所以,最终的XML将是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<AddCustomerRequest APIVersion="2.0" Name="Add Customers List Data">
    <CFParameters>
        <ReqId>1-YNU68</ReqId>
        <CfgId>1-TSP13</CfgId>
    </CFParameters>
    <Parameters>
        <ParName>Test Customer List</ParName>
        <ListName>Test Customer List</ListName>
        <Summary>N</Summary>
        <CustomerList>
            <CustomerInfo>
                <LoadID>1-YNU4R</LoadID>
                <CustomerID>1-E0FB</CustomerID>
                <LoadConID>1-YTQJ5</LoadConID>
                <TimeZone></TimeZone>
                <Available>Y</Available>
                <Phones>
                    <Phone PhoneType="4">6987999912</Phone>
                    <Phone PhoneType="2">6987991657</Phone>
                    <Phone PhoneType="1">6987991152</Phone>
                    <Phone PhoneType="3">6987999278</Phone>
                </Phones>
                <CustomFields></CustomFields>
            </CustomerInfo>
        </CustomerList>
    </Parameters>
</AddCustomerRequest>

我尝试使用以下XSL执行上述所有操作但是虽然排序有效,但它不会替换值(因此排序错误):

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

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

        <xsl:template match="@Phones">
          <xsl:attribute name="PhoneType">
            <xsl:choose>
              <xsl:when test=". = 1">
                <xsl:text>103</xsl:text>
              </xsl:when>
              <xsl:when test=". = 2">
                <xsl:text>102</xsl:text>
              </xsl:when>
              <xsl:when test=". = 3">
                <xsl:text>104</xsl:text>
              </xsl:when>
              <xsl:when test=". = 4">
                <xsl:text>101</xsl:text>
              </xsl:when>
            </xsl:choose>
          </xsl:attribute>
        </xsl:template>


        <xsl:template match="Phones">
            <xsl:copy>
                <xsl:apply-templates select="Phone">
                    <xsl:sort select="@PhoneType"/>
                </xsl:apply-templates>
            </xsl:copy>
        </xsl:template>

        <xsl:template match="@Phones">
          <xsl:attribute name="PhoneType">
            <xsl:choose>
              <xsl:when test=". = 103">
                <xsl:text>1</xsl:text>
              </xsl:when>
              <xsl:when test=". = 102">
                <xsl:text>2</xsl:text>
              </xsl:when>
              <xsl:when test=". = 104">
                <xsl:text>3</xsl:text>
              </xsl:when>
              <xsl:when test=". = 101">
                <xsl:text>4</xsl:text>
              </xsl:when>
            </xsl:choose>
          </xsl:attribute>
        </xsl:template>

</xsl:stylesheet>

有人可以帮忙吗?

由于 乔治

1 个答案:

答案 0 :(得分:1)

简单地说:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="Phones">
    <xsl:copy>
        <xsl:apply-templates select="Phone">
        <xsl:sort select="string-length(substring-before('4213', @PhoneType))" data-type="number" order="ascending"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

P.S。

<xsl:template match="@Phones">

与XML中的任何内容都不匹配。你当然不能有两个匹配同一组节点的模板;只会应用其中一个。

为了执行您的宏计划,您必须(1)将Phone个节点写入变量,同时将PhoneType替换为另一个字符串; (2)将该变量转换为节点集; (3)对节点集进行排序并将其写入输出,同时用原始值替换PhoneType