XSLT - 访问集合中的元素
XSLT - accessing elements within a collection
我正在尝试将目录 ('output') 中的多个 XML 文件合并到不同目录 ('combine') 中的一个文件中。 xml 文件是非常简单的站点地图:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>https://blahblah.com/blah</loc>
<changefreq>weekly</changefreq>
</url>
<url>
<loc>https://blahblah.com/blah/blah</loc>
<changefreq>weekly</changefreq>
</url>
</urlset>
我可以成功地使用 Collection() 来检索文件的内容,但是一旦这些文件在集合中,我似乎就无法访问这些文件中的任何元素。
我的 XSLT 如下所示:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template name="main">
<xsl:variable name="collection" select="collection('output?recurse=yes;select=*.xml')/*"/>
<xsl:variable name="loc" select="distinct-values($collection/urlset/url/loc)"/>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<xsl:for-each select="$loc">
<url>
<loc>
<xsl:value-of select="."/>
</loc>
</url>
</xsl:for-each>
</urlset>
</xsl:template>
</xsl:stylesheet>
目前输出..不是很多。
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"/>
如果我将 <xsl:for-each select="$loc">
更改为 <xsl:for-each select="$collection">
,它会输出每个文件的内容,因此我知道它可以访问该集合。但是,如果我尝试直接访问集合文件中的任何元素,无论是通过上面的 distinct-values
,还是通过各种不同的引用(见下文),它都不会显示任何内容。
我试过:
<xsl:for-each select="$collection/urlset/url/loc">
<url>
<loc>
<xsl:value-of select="."/>
</loc>
</url>
</xsl:for-each>
和
<xsl:for-each select="$collection">
<url>
<loc>
<xsl:value-of select="urlset/url/loc"/>
</loc>
</url>
</xsl:for-each>
和
<xsl:for-each select="$loc">
<url>
<loc>
<xsl:value-of select="($collection/urlset/url[loc=current()])"/>
</loc>
</url>
</xsl:for-each>
除了输出集合中至少显示可以访问文件的所有内容外,没有任何效果:
<xsl:for-each select="$collection">
<url>
<loc>
<xsl:value-of select="."/>
</loc>
</url>
</xsl:for-each>
如果有帮助,这是我正在使用的撒克逊命令:
java -jar c:\saxon\SaxonHE9-9-1-5J\saxon9he.jar -o:C:\Code\photo.old\xml\sitemap\combine\output.xml -xsl:C:\Code\photo.old\xml\sitemap\combine_s.xslt -it:main
编辑-已解决!
两件事的结合:
- Martin 在下面的回复:您需要将 xpath-default-namespace 设置为源文件中的命名空间:
xpath-default-namespace="http://www.sitemaps.org/schemas/sitemap/0.9"
- 无论出于何种原因,集合变量都无法使用不同的值。我需要直接在 distinct-values 命令中定义和访问集合:
<xsl:variable name="loc" select="distinct-values(collection('output?strip-space=yes;select=*.xml')/urlset/url/loc)"/>
我想,如果您在命名空间 xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
中有元素,您希望在 XSLT 中使用 xpath-default-namespace="http://www.sitemaps.org/schemas/sitemap/0.9"
。否则,您的 urlset/url/loc
之类的路径将 select 这些名称的元素不在任何命名空间中。
我正在尝试将目录 ('output') 中的多个 XML 文件合并到不同目录 ('combine') 中的一个文件中。 xml 文件是非常简单的站点地图:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>https://blahblah.com/blah</loc>
<changefreq>weekly</changefreq>
</url>
<url>
<loc>https://blahblah.com/blah/blah</loc>
<changefreq>weekly</changefreq>
</url>
</urlset>
我可以成功地使用 Collection() 来检索文件的内容,但是一旦这些文件在集合中,我似乎就无法访问这些文件中的任何元素。
我的 XSLT 如下所示:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template name="main">
<xsl:variable name="collection" select="collection('output?recurse=yes;select=*.xml')/*"/>
<xsl:variable name="loc" select="distinct-values($collection/urlset/url/loc)"/>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<xsl:for-each select="$loc">
<url>
<loc>
<xsl:value-of select="."/>
</loc>
</url>
</xsl:for-each>
</urlset>
</xsl:template>
</xsl:stylesheet>
目前输出..不是很多。
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"/>
如果我将 <xsl:for-each select="$loc">
更改为 <xsl:for-each select="$collection">
,它会输出每个文件的内容,因此我知道它可以访问该集合。但是,如果我尝试直接访问集合文件中的任何元素,无论是通过上面的 distinct-values
,还是通过各种不同的引用(见下文),它都不会显示任何内容。
我试过:
<xsl:for-each select="$collection/urlset/url/loc">
<url>
<loc>
<xsl:value-of select="."/>
</loc>
</url>
</xsl:for-each>
和
<xsl:for-each select="$collection">
<url>
<loc>
<xsl:value-of select="urlset/url/loc"/>
</loc>
</url>
</xsl:for-each>
和
<xsl:for-each select="$loc">
<url>
<loc>
<xsl:value-of select="($collection/urlset/url[loc=current()])"/>
</loc>
</url>
</xsl:for-each>
除了输出集合中至少显示可以访问文件的所有内容外,没有任何效果:
<xsl:for-each select="$collection">
<url>
<loc>
<xsl:value-of select="."/>
</loc>
</url>
</xsl:for-each>
如果有帮助,这是我正在使用的撒克逊命令:
java -jar c:\saxon\SaxonHE9-9-1-5J\saxon9he.jar -o:C:\Code\photo.old\xml\sitemap\combine\output.xml -xsl:C:\Code\photo.old\xml\sitemap\combine_s.xslt -it:main
编辑-已解决!
两件事的结合:
- Martin 在下面的回复:您需要将 xpath-default-namespace 设置为源文件中的命名空间:
xpath-default-namespace="http://www.sitemaps.org/schemas/sitemap/0.9"
- 无论出于何种原因,集合变量都无法使用不同的值。我需要直接在 distinct-values 命令中定义和访问集合:
<xsl:variable name="loc" select="distinct-values(collection('output?strip-space=yes;select=*.xml')/urlset/url/loc)"/>
我想,如果您在命名空间 xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
中有元素,您希望在 XSLT 中使用 xpath-default-namespace="http://www.sitemaps.org/schemas/sitemap/0.9"
。否则,您的 urlset/url/loc
之类的路径将 select 这些名称的元素不在任何命名空间中。