【问题标题】:XML file not displaying attributes after transformation转换后 XML 文件不显示属性
【发布时间】:2022-01-13 01:08:09
【问题描述】:

我正在创建一个 XSL 文件,它将接受一个参数并显示来自 XML 文档的某些信息,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<customer name="Evans, Terry"
      state="OH"
      zip="44660"
      orders="3"
      number_items="23"/>

此人的 cid 值为 c5391。

XML 文件是这样的:

<?xml version="1.0" encoding="UTF-8"?>

<customers>
    <customer cid="c5391">
        <name>Evans, Terry</name>
        <street>641 Greenway Blvd.</street>
        <city>Mount Hope</city>
        <state>OH</state>
        <zip>44660</zip>
        <orders>
            <order oid="52517">
                <date>8/1/2017</date>
                <item iid="wb7133">
                    <description>Insulated Water Bottle</description>
                    <qty>2</qty>
                </item>
                <item iid="gps1015">
                    <description>Zendo GPS meter</description>
                    <qty>1</qty>
                </item>
                <item iid="bl2815">
                    <description>Boot Laces (Medium)</description>
                    <qty>1</qty>
                </item>
                <item iid="tr8140">
                    <description>Trail Mix (Pouch)</description>
                    <qty>5</qty>
                </item>
                <item iid="fa8442">
                    <description>First Aid Kit (Pack Size)</description>
                    <qty>1</qty>
                </item>
                <item iid="bb7117">
                    <description>Blister Patches</description>
                    <qty>3</qty>
                </item>
            </order>
            <order oid="53003">
                <date>8/5/2017</date>
                <item iid="hp7814">
                    <description>Fiberglass Light Hiking Poles (Spring Adj.)</description>
                    <qty>1</qty>
                </item>
            </order>
            <order oid="54814">
                <date>8/6/2017</date>
                <item iid="sb6601">
                    <description>Solar Battery Recharging Unit</description>
                    <qty>1</qty>
                </item>
                <item iid="br9002">
                    <description>Bug Repellent (Deep Woodes)</description>
                    <qty>2</qty>
                </item>
                <item iid="sb8502">
                    <description>Sunblock SPF 30 (Hiking Size)</description>
                    <qty>6</qty>
                </item>
            </order>
        </orders>
    </customer>

我的 XSL 文件是这样的:

<?xml version="1.0" encoding="UTF-8" ?>

<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:param name="cid" select="'c5391'" />

    <xsl:variable name="custList" select="/customers/customer[@cid = $cid]" />

    <xsl:template match="/">
        <customer name="{name}"
            state="{state}"
            zip="{zip}"
            orders="count($custList/orders/order)"
            number_items="format-number(sum($custList/orders/order/item/qty), '##.##')" />
    </xsl:template>

</xsl:stylesheet>

我的输出文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<customer name=""
      state=""
      zip=""
      orders="count($custList/orders/order)"
      number_items="format-number(sum($custList/orders/order/item/qty), '##.##')"/>

我无法弄清楚为什么没有从 XML 文件中提取这些值。我必须进行哪些更改才能使我的输出文件看起来像我帖子开头的示例?

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    也许您想简单地匹配客户,而不是这样:

    <xsl:variable name="custList" select="/customers/customer[@cid = $cid]" />
    <xsl:template match="/">
    ....
    

    也许你想要这个:

    <xsl:template match="/customers/customer">
        <xsl:if test="@cid = $cid">
        ....
    

    这将为您提供客户,然后从那里输出内容。

    您在该模板中还有其他需要 {} 的问题。但是这个 XSL:

    <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:param name="cid" select="'c5391'" />
        <xsl:template match="/customers/customer">
            <xsl:if test="@cid = $cid">
            <customer name="{name}"
                state="{state}"
                zip="{zip}"
                orders="{count(orders/order)}"
                number_items="{format-number(sum(orders/order/item/qty), '##.##')}" />
            </xsl:if>
        </xsl:template>
    </xsl:stylesheet>
    

    产生这个输出:

    <customer name="Evans, Terry" state="OH" zip="44660" orders="3" number_items="23"/>
    

    【讨论】:

    • 由于我收到的具体指令,我需要匹配root并使用一个变量。如果不是这些规则,您的解决方案绝对可以满足我的需求。您的回答确实让我意识到我需要将变量放入属性中,并且我需要更多的花括号。谢谢!
    【解决方案2】:

    原来我只需要在我试图显示的元素之前将 $custList 添加到花括号中。大脑袋屁。所以,而不是这个:

    <xsl:template match="/">
        <customer name="{name}"
            state="{state}"
            zip="{zip}"
            orders="count($custList/orders/order)"
            number_items="format-number(sum($custList/orders/order/item/qty), '##.##')" />
    </xsl:template>
    

    我这样做了:

    <xsl:template match="/">
        <customer name="{$custList/name}"
            state="{$custList/state}"
            zip="{$custList/zip}"
            orders="{count($custList/$ordList)}"
            number_items="{sum($custList/$ordList/item/qty)}" />
    </xsl:template>
    

    我收到了我想要的输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 2017-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多