在 Xpath 字符串中传递变量
Pass a variable in an Xpath String
我有一个 xsl 文件,其中包含一些 xPath..
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" encoding="UTF-8"/>
<xsl:variable name="ref_num" select="ABCD"/>
<xsl:template match="holiday">
<fromPrice>
<xsl:copy-of select="document('/file/to/path/in/documents/FILE1.xml',/)/dataroot/Tour/RefNo[.='ABHC']/preceding-sibling::AXL "/>
</fromPrice>
</xsl:template>
</xsl:stylesheet>
场景..
xml 文件从 FILE1.xml
(其中的值来自 <AXL>
元素)文件正确返回 <fromPrice>
值,如下所示:
FILE1.xml
<Tour>
<Allocation>0</Allocation>
<Available>16</Available>
<AXL>4658</AXL>
<ALO>2195</ALO>
<RefNo>AABC</RefNo>
</Tour>
<Tour>
<Allocation>0</Allocation>
<Available>16</Available>
<AXL>1295</AXL>
<ALO>595</ALO>
<RefNo>ATSS</RefNo>
</Tour>
问题:
我希望 $ref_num
在 "/RefNo[.='ABHC']"
的 xPath 中传递,以便 RefNo
包含变量 $ref_num
的值 ABCD
。这样,如果 $ref_num
发生变化,我就不必再次对 "/RefNo[.='ABHC']"
处的 xPath 进行硬编码。
到目前为止,我尝试了以下方法:
* /RefNo[.=' "+$ref_num+ "']
但这没有用。{样式表错误。无法编译}
* /RefNo[.=' \"+$ref_num+ \"']
但这也不起作用。{样式表错误。无法编译}
* /RefNo[.='+$ref_num+']
但这也行不通。{样式表错误。无法编译}
我看不出哪里有问题,只需在表达式中使用变量即可。
<xsl:variable name="ref_num" select="AABC" />
<xsl:variable name="filePath" select="'/file/to/path/in/documents/FILE1.xml'" />
<xsl:variable name="tours" select="document($filePath)/dataroot" />
<xsl:template match="holiday">
<fromPrice>
<xsl:copy-of select="$tours/Tour[RefNo = $ref_num]/AXL" />
</fromPrice>
</xsl:template>
提示:XPath 表达式不是字符串。这不是插值。
我有一个 xsl 文件,其中包含一些 xPath..
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" encoding="UTF-8"/>
<xsl:variable name="ref_num" select="ABCD"/>
<xsl:template match="holiday">
<fromPrice>
<xsl:copy-of select="document('/file/to/path/in/documents/FILE1.xml',/)/dataroot/Tour/RefNo[.='ABHC']/preceding-sibling::AXL "/>
</fromPrice>
</xsl:template>
</xsl:stylesheet>
场景..
xml 文件从 FILE1.xml
(其中的值来自 <AXL>
元素)文件正确返回 <fromPrice>
值,如下所示:
FILE1.xml
<Tour>
<Allocation>0</Allocation>
<Available>16</Available>
<AXL>4658</AXL>
<ALO>2195</ALO>
<RefNo>AABC</RefNo>
</Tour>
<Tour>
<Allocation>0</Allocation>
<Available>16</Available>
<AXL>1295</AXL>
<ALO>595</ALO>
<RefNo>ATSS</RefNo>
</Tour>
问题:
我希望 $ref_num
在 "/RefNo[.='ABHC']"
的 xPath 中传递,以便 RefNo
包含变量 $ref_num
的值 ABCD
。这样,如果 $ref_num
发生变化,我就不必再次对 "/RefNo[.='ABHC']"
处的 xPath 进行硬编码。
到目前为止,我尝试了以下方法:
* /RefNo[.=' "+$ref_num+ "']
但这没有用。{样式表错误。无法编译}
* /RefNo[.=' \"+$ref_num+ \"']
但这也不起作用。{样式表错误。无法编译}
* /RefNo[.='+$ref_num+']
但这也行不通。{样式表错误。无法编译}
我看不出哪里有问题,只需在表达式中使用变量即可。
<xsl:variable name="ref_num" select="AABC" />
<xsl:variable name="filePath" select="'/file/to/path/in/documents/FILE1.xml'" />
<xsl:variable name="tours" select="document($filePath)/dataroot" />
<xsl:template match="holiday">
<fromPrice>
<xsl:copy-of select="$tours/Tour[RefNo = $ref_num]/AXL" />
</fromPrice>
</xsl:template>
提示:XPath 表达式不是字符串。这不是插值。