xsl:for-each select 不适用于 XML 中的 xmlns="http://www.example.com/TBD"
xsl:for-each select does not work with xmlns="http://www.example.com/TBD" inside XML
我的 XSLT-2.0 代码需要一些帮助。
这是 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:param name="today" select="format-dateTime(current-dateTime(), '[Y0001]-[M01]-[D01]T[H1]:[m01]:[s01]')"/>
<document>
<xsl:for-each select="//Product">
<Product>
<Productdetail>
<DocumentID>
<xsl:value-of select="./ProductNr"/>
</DocumentID>
<DocumentProd>
<xsl:value-of select=".//Account/DebitNr"/>
</DocumentProd>
<Date>
<xsl:value-of select="$today"/>
</Date>
</Productdetail>
</Product>
</xsl:for-each>
</document>
</xsl:template>
</xsl:stylesheet>
这里是xml
<?xml version="1.0" encoding="UTF-8"?>
<Products refSchemaFile="V11.xsd" xmlns="http://www.example.com/TBD">
<Product>
<Nr/>
<ProductNr>837228</ProductNr>
<Account>
<Nr/>
<DebitNr>21549</DebitNr>
</Account>
</Product>
<Product>
<Nr/>
<ProductNr>837227</ProductNr>
<Account>
<Nr/>
<DebitNr>21547</DebitNr>
</Account>
</Product>
<Product>
<Nr/>
<ProductNr>837226</ProductNr>
<Account>
<Nr/>
<DebitNr>21544</DebitNr>
</Account>
</Product>
</Products>
这是 XSLT 结果
<?xml version="1.0" encoding="UTF-8"?>
<document xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema"/>
但是如果我从 XML 中删除 xmlns="http://www.example.com/TBD"
,那么我会得到:
<?xml version="1.0" encoding="UTF-8"?>
<document xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<Product>
<Productdetail>
<DocumentID>837228</DocumentID>
<DocumentProd>21549</DocumentProd>
<Date>2021-02-21T22:40:18</Date>
</Productdetail>
</Product>
<Product>
<Productdetail>
<DocumentID>837227</DocumentID>
<DocumentProd>21547</DocumentProd>
<Date>2021-02-21T22:40:18</Date>
</Productdetail>
</Product>
<Product>
<Productdetail>
<DocumentID>837226</DocumentID>
<DocumentProd>21544</DocumentProd>
<Date>2021-02-21T22:40:18</Date>
</Productdetail>
</Product>
</document>
为什么 xmlns="http://www.example.com/TBD"
是个问题,我必须怎么做才能在 XML 中创建带有 xmlns="http://www.example.com/TBD"
的结果?
XML 中的 xmlns=".."
指令设置该元素及其所有子元素的名称空间。命名空间通常使用元素名称的前缀来实现,例如 xmlns:fn="http://www.w3.org/2005/xpath-functions"
。要使用此命名空间中的元素,您必须使用 <fn:funcXY>
,例如
在您的情况下,将以下属性添加到 XSLT 的 xsl:stylesheet
元素就足够了:
xpath-default-namespace="http://www.example.com/TBD"
它为所有未使用第一种方法明确指定另一个命名空间的 XPath 表达式设置命名空间。因此,这些表达式将在不为所有匹配表达式设置前缀的情况下进行匹配。
例如,您可以阅读有关命名空间的更多信息 here at this link。
我的 XSLT-2.0 代码需要一些帮助。
这是 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:param name="today" select="format-dateTime(current-dateTime(), '[Y0001]-[M01]-[D01]T[H1]:[m01]:[s01]')"/>
<document>
<xsl:for-each select="//Product">
<Product>
<Productdetail>
<DocumentID>
<xsl:value-of select="./ProductNr"/>
</DocumentID>
<DocumentProd>
<xsl:value-of select=".//Account/DebitNr"/>
</DocumentProd>
<Date>
<xsl:value-of select="$today"/>
</Date>
</Productdetail>
</Product>
</xsl:for-each>
</document>
</xsl:template>
</xsl:stylesheet>
这里是xml
<?xml version="1.0" encoding="UTF-8"?>
<Products refSchemaFile="V11.xsd" xmlns="http://www.example.com/TBD">
<Product>
<Nr/>
<ProductNr>837228</ProductNr>
<Account>
<Nr/>
<DebitNr>21549</DebitNr>
</Account>
</Product>
<Product>
<Nr/>
<ProductNr>837227</ProductNr>
<Account>
<Nr/>
<DebitNr>21547</DebitNr>
</Account>
</Product>
<Product>
<Nr/>
<ProductNr>837226</ProductNr>
<Account>
<Nr/>
<DebitNr>21544</DebitNr>
</Account>
</Product>
</Products>
这是 XSLT 结果
<?xml version="1.0" encoding="UTF-8"?>
<document xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema"/>
但是如果我从 XML 中删除 xmlns="http://www.example.com/TBD"
,那么我会得到:
<?xml version="1.0" encoding="UTF-8"?>
<document xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<Product>
<Productdetail>
<DocumentID>837228</DocumentID>
<DocumentProd>21549</DocumentProd>
<Date>2021-02-21T22:40:18</Date>
</Productdetail>
</Product>
<Product>
<Productdetail>
<DocumentID>837227</DocumentID>
<DocumentProd>21547</DocumentProd>
<Date>2021-02-21T22:40:18</Date>
</Productdetail>
</Product>
<Product>
<Productdetail>
<DocumentID>837226</DocumentID>
<DocumentProd>21544</DocumentProd>
<Date>2021-02-21T22:40:18</Date>
</Productdetail>
</Product>
</document>
为什么 xmlns="http://www.example.com/TBD"
是个问题,我必须怎么做才能在 XML 中创建带有 xmlns="http://www.example.com/TBD"
的结果?
XML 中的 xmlns=".."
指令设置该元素及其所有子元素的名称空间。命名空间通常使用元素名称的前缀来实现,例如 xmlns:fn="http://www.w3.org/2005/xpath-functions"
。要使用此命名空间中的元素,您必须使用 <fn:funcXY>
,例如
在您的情况下,将以下属性添加到 XSLT 的 xsl:stylesheet
元素就足够了:
xpath-default-namespace="http://www.example.com/TBD"
它为所有未使用第一种方法明确指定另一个命名空间的 XPath 表达式设置命名空间。因此,这些表达式将在不为所有匹配表达式设置前缀的情况下进行匹配。
例如,您可以阅读有关命名空间的更多信息 here at this link。