【问题标题】:XSLT Change Attribute Values and SortXSLT 更改属性值和排序
【发布时间】:2015-05-27 14:39:18
【问题描述】:

我是 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 值等价于以下内容: 电话类型“1” = 业务 电话类型“2”=家庭 电话类型“3” = 其他 PhoneType “4” = 手机

请求是按以下 PhoneType 对 Phones 进行排序: 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 值替换为原始值

例如:

            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>

有人可以帮忙吗?

谢谢 乔治

【问题讨论】:

    标签: xml xslt xml-parsing


    【解决方案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>
    

    附言

    <xsl:template match="@Phones">
    

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

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

    【讨论】:

    • 感谢您的快速响应!它工作得很好。看起来很简单,但我永远不会想到这个解决方案(刚刚进入 XSLT 世界),我真的非常感谢你!!!
    猜你喜欢
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 2010-10-11
    相关资源
    最近更新 更多