XSLT document()未调用特定节点

问题描述 投票:0回答:2

我正在尝试在我的xsl代码中调用'firstName'节点,但是当我尝试调用'value-of select =“ firstName'时,它似乎不起作用,并且一旦转换,它就不会显示任何内容它。

我的xsl代码是:

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

    <xsl:variable name="customers" select="document('customer.xml')/customers"/>

    <xsl:template match="/">
        <xsl:copy>
            <xsl:for-each select="transactions/transaction">
                <xsl:sort select="giftShop"/>
                <xsl:sort select="transaction_date"/>
                Shop: <xsl:value-of select="giftShop"/>
                Date: <xsl:value-of select="transaction_date"/>
                <xsl:for-each select="$customers">
                    <xsl:value-of select="."/>
                </xsl:for-each>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

transactions.xml(第一个xml文件::

<?xml version="1.0"?>
<transactions>
    <transaction>
        <transaction_date>01/11/2019</transaction_date>
        <customerID>8BLOJZWL</customerID>
        <giftShop>1</giftShop>
        <transactionID>BjYAtd7lmEOlQUVy</transactionID>
        <value currency="gbp">149.99</value>
    </transaction>
    <transaction>
        <transaction_date>01/11/2019</transaction_date>
        <customerID>DR18OCFI</customerID>
        <giftShop>2</giftShop>
        <transactionID>aQ27lMvfnlzY4mkx</transactionID>
        <value currency="gbp">100.11</value>
    </transaction>
<transaction>

customer.xml(第二个xml文件::

<?xml version="1.0"?>
<customers>
    <customer>
        <prefix>Mrs</prefix>
        <lastName>Samantha</lastName>
        <givenName>Smith</givenName>
        <addressID>213456</addressID>
        <customerID>ASJ4OTLG</customerID>
    </customer>
    <customer>
        <prefix>Mr </prefix>
        <lastName>Cameron</lastName>
        <givenName>Wills</givenName>
        <addressID>125907</addressID>
        <customerID>SID8RY23</customerID>
    </customer>
</customers>

预期输出:也很抱歉,如果我的预期输出不是很准确,我对xml和xsl还是很陌生

<giftShop>
    <transaction_date>
        <transacation>
            <customer>

我正在尝试调用特定的'客户数据',但目前它显示所有数据,并在我尝试关闭文件时不断使我的Notepad ++崩溃。

非常感谢任何帮助!

xml xslt transform document
2个回答
0
投票

由于您的文档变量已经引用了“客户”

<xsl:variable name="customers" select="document('customer.xml')/customers"/>

[当您进行for循环时

<xsl:for-each select="$customers">
  <xsl:value-of select="."/>
</xsl:for-each>

您应该做这样的事情

<xsl:for-each select="$customers/customer">
  <xsl:value-of select="customerID"/> <!-- Or whichever node you want -->
</xsl:for-each>

0
投票

我正在猜测(!),您想做这样的事情:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:variable name="customers" select="document('retail_customer.xml')/customers"/>

<xsl:template match="/transactions">
    <xsl:for-each select="transaction">
        <xsl:sort select="giftShop"/>
        <xsl:sort select="transaction_date"/>
        <xsl:text>Shop: </xsl:text>
        <xsl:value-of select="giftShop"/>
        <xsl:text>&#10;Date: </xsl:text>
        <xsl:value-of select="transaction_date"/>
        <xsl:variable name="customer" select="$customers/customer[customerID=current()/customerID]"/>
        <xsl:text>&#10;Customer: </xsl:text>
        <xsl:value-of select="$customer/lastName"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="$customer/givenName"/>
        <xsl:text>&#10;&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

当然,只有在其他文档中交易具有匹配的客户时,此选项才有效。在您的示例中,没有。

© www.soinside.com 2019 - 2024. All rights reserved.