使用 xpath 和 xsl 遍历 xml 文档
Traverse xml document using xpath and xsl
我正在使用 XSL 和 XPATH 遍历、读取和转换一个复杂的 XML 文件,其中我有多个名称相同但值不同的节点。我的示例 xml 文件如下 -
<core>
<address>
<postalZipCode>90017</postalZipCode>
<updateSource>xxxxxx</updateSource>
<city>LOS ANGELES</city>
<stateProvince>CA</stateProvince>
<type>MAILING</type>
<line1>818 WEST SEVENTH STREET</line1>
</address>
<address>
<postalZipCode>95014</postalZipCode>
<updateSource>xxxxxx</updateSource>
<city>CUPERTINO</city>
<stateProvince>CA</stateProvince>
<type>PRIMARY</type>
<line1>1234 XYZ STREET</line1>
</address>
<memberId>0</memberId>
</core>
当我读取 MAILING ADDRESS Line 1 值时,我得到的值是“1234 XYZ STREET”,这实际上是 PRIMARY ADDRESS。这是我的 xsl 文件片段:-
<xsl:template match="core">
<xsl:if test="memberId['0'] and address/type['MAILING']">
<fo:table-row>
<fo:table-cell><fo:block /></fo:table-cell>
<fo:table-cell xsl:use-attribute-sets="data">
<fo:block>Line 1</fo:block>
</fo:table-cell>
<fo:table-cell xsl:use-attribute-sets="data">
<fo:block><xsl:value-of select="address/line1/text()"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
这里的专家能否建议是否有任何其他方法可以强制 XSL 读取地址为 MAILING 的正确值,而不是读取它找到的第一个值???
if there is any other way possible to force XSL to read the correct
value of where address is MAILING, instead of reading the first value
it finds ???
您的问题相当混乱,因为 MAILING 是您示例中的第一个地址 - 而您显示的代码段确实 不 产生结果你说是。
无论如何,要确保您从 MAILING 地址获取数据而不管其位置如何,请更改:
<xsl:value-of select="address/line1/text()"/>
至:
<xsl:value-of select="address[type='MAILING']/line1"/>
要了解其工作原理,请阅读有关 predicates 的更多信息。
您编写的代码显示 "if there is an address with type MAILING, then print the value of address/line1 (whether or not this is in any way related to the address with type MAILING)"。
在 XSLT 1.0 中,当 xsl:value-of
select 有多个节点时,它会显示它找到的第一个节点的值。在 XSLT 2.0 中,它将所有 selected 节点的值与默认为单个 space.
的分隔符连接起来
如果有多个节点并且您想 select 一个特定的节点,那么 select 它带有一个谓词,例如xsl:value-of select="address[@type='MAILING']/line1
.
我正在使用 XSL 和 XPATH 遍历、读取和转换一个复杂的 XML 文件,其中我有多个名称相同但值不同的节点。我的示例 xml 文件如下 -
<core>
<address>
<postalZipCode>90017</postalZipCode>
<updateSource>xxxxxx</updateSource>
<city>LOS ANGELES</city>
<stateProvince>CA</stateProvince>
<type>MAILING</type>
<line1>818 WEST SEVENTH STREET</line1>
</address>
<address>
<postalZipCode>95014</postalZipCode>
<updateSource>xxxxxx</updateSource>
<city>CUPERTINO</city>
<stateProvince>CA</stateProvince>
<type>PRIMARY</type>
<line1>1234 XYZ STREET</line1>
</address>
<memberId>0</memberId>
</core>
当我读取 MAILING ADDRESS Line 1 值时,我得到的值是“1234 XYZ STREET”,这实际上是 PRIMARY ADDRESS。这是我的 xsl 文件片段:-
<xsl:template match="core">
<xsl:if test="memberId['0'] and address/type['MAILING']">
<fo:table-row>
<fo:table-cell><fo:block /></fo:table-cell>
<fo:table-cell xsl:use-attribute-sets="data">
<fo:block>Line 1</fo:block>
</fo:table-cell>
<fo:table-cell xsl:use-attribute-sets="data">
<fo:block><xsl:value-of select="address/line1/text()"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
这里的专家能否建议是否有任何其他方法可以强制 XSL 读取地址为 MAILING 的正确值,而不是读取它找到的第一个值???
if there is any other way possible to force XSL to read the correct value of where address is MAILING, instead of reading the first value it finds ???
您的问题相当混乱,因为 MAILING 是您示例中的第一个地址 - 而您显示的代码段确实 不 产生结果你说是。
无论如何,要确保您从 MAILING 地址获取数据而不管其位置如何,请更改:
<xsl:value-of select="address/line1/text()"/>
至:
<xsl:value-of select="address[type='MAILING']/line1"/>
要了解其工作原理,请阅读有关 predicates 的更多信息。
您编写的代码显示 "if there is an address with type MAILING, then print the value of address/line1 (whether or not this is in any way related to the address with type MAILING)"。
在 XSLT 1.0 中,当 xsl:value-of
select 有多个节点时,它会显示它找到的第一个节点的值。在 XSLT 2.0 中,它将所有 selected 节点的值与默认为单个 space.
如果有多个节点并且您想 select 一个特定的节点,那么 select 它带有一个谓词,例如xsl:value-of select="address[@type='MAILING']/line1
.