我试图找出为什么这个 XSLT 没有按照我期望的方式工作。简而言之,我想将一组 XML 元素存储在变量中,然后稍后访问它们。以下是问题的示例。
给定输入 XML:
<wd:Report_Data xmlns:wd="urn:com.workday/bsvc">
<wd:Report_Entry>
<wd:Employee_ID>123</wd:Employee_ID>
<wd:Primary_Address>
<wd:Address_Line_1>PO BOY 123</wd:Address_Line_1>
<wd:Address_Line_2>12345 Road Street</wd:Address_Line_2>
<wd:City>CityTown</wd:City>
<wd:Use_For>
<wd:type>PHYSICAL_ADDRESS</wd:type>
</wd:Use_For>
</wd:Primary_Address>
</wd:Report_Entry>
</wd:Report_Data>
...以及这样的变换:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wd="urn:com.workday/bsvc">
<xsl:output method="text" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="wd:Report_Data/wd:Report_Entry"/>
</xsl:template>
<xsl:template match="wd:Report_Entry">
<xsl:variable name="PrimaryAddress">
<xsl:copy-of select="wd:Primary_Address"/>
</xsl:variable>
<xsl:value-of select="$PrimaryAddress/wd:Address_Line_1"/>
</xsl:template>
</xsl:stylesheet>
我希望得到类似:“PO BOY 123”的信息,但我什么也没得到。当我更改 value-of 语句以仅选择变量时,如下所示:“
PO BOY 123
12345 Road Street
CityTown
PHYSICAL_ADDRESS
如何使用我创建的变量来访问“Address_Line_1”等子元素?
魔法起作用了......当我发布这个问题时,我偶然发现了另一个答案。我不确定这是否是“正确”答案,因此如果您有更多信息,请提供另一个答案。
我所做的只是将变量声明更改为“
添加“as='element()'”属性似乎已经成功了。现在我可以按照预期使用 XPath 表达式浏览变量。