You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求XSLT3.0与XPath3.1中数组和Map的可用示例代码

Great to see you're working with XSLT 3.0 and XPath 3.1 features in Saxon HE! Your setup looks solid, though I noticed a small typo in your system property calls (trailing spaces in the property names like 'xsl:version ' will return empty values—easy fix!).

Here's a complete, tested example that demonstrates both arrays and maps, with explanations of key features:

<xsl:stylesheet version="3.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:map="http://www.w3.org/2005/xpath-functions/map">
    <xsl:output method="text" indent="yes"/>

    <xsl:template match="/">
        <!-- Display system properties (fixed the trailing space issue!) -->
        <xsl:text>XSLT Version: </xsl:text>
        <xsl:value-of select="system-property('xsl:version')"/>
        <xsl:text>&#10;Vendor: </xsl:text>
        <xsl:value-of select="system-property('xsl:vendor')"/>
        <xsl:text>&#10;XPath Version: </xsl:text>
        <xsl:value-of select="system-property('xsl:xpath-version')"/>
        <xsl:text>&#10;&#10;</xsl:text>

        <!-- 1. Working with Arrays -->
        <xsl:text>=== Array Examples ===&#10;</xsl:text>
        <!-- Create an array of strings (explicit type declaration) -->
        <xsl:variable name="fruit-array" as="array(xs:string)" select="['Apple', 'Banana', 'Cherry']"/>
        <!-- Access first element (arrays are 0-indexed!) -->
        <xsl:text>First fruit: </xsl:text>
        <xsl:value-of select="$fruit-array(0)"/>
        <xsl:text>&#10;</xsl:text>
        <!-- Convert array to sequence and iterate over all elements -->
        <xsl:text>All fruits: </xsl:text>
        <xsl:value-of select="$fruit-array?*" separator=", "/>
        <xsl:text>&#10;&#10;</xsl:text>

        <!-- 2. Working with Maps -->
        <xsl:text>=== Map Examples ===&#10;</xsl:text>
        <!-- Create a map with mixed value types (string, number, nested array) -->
        <xsl:variable name="user-map" as="map(xs:string, item())" select="map {
            'name': 'Alice',
            'age': 30,
            'hobbies': ['Reading', 'Hiking']
        }"/>
        <!-- Access individual map entries -->
        <xsl:text>User name: </xsl:text>
        <xsl:value-of select="$user-map('name')"/>
        <xsl:text>&#10;User age: </xsl:text>
        <xsl:value-of select="$user-map('age')"/>
        <xsl:text>&#10;</xsl:text>
        <!-- Iterate over all map key-value pairs -->
        <xsl:text>All map entries:&#10;</xsl:text>
        <xsl:for-each select="$user-map?*">
            <xsl:value-of select="map:entry-key(.) || ': ' || map:entry-value(.)"/>
            <xsl:text>&#10;</xsl:text>
        </xsl:for-each>
        <!-- Access nested array inside the map -->
        <xsl:text>User's first hobby: </xsl:text>
        <xsl:value-of select="$user-map('hobbies')(0)"/>
    </xsl:template>
</xsl:stylesheet>

What this example demonstrates:

  • Arrays: We create a typed array of strings, access elements via 0-based index, and convert the array to a sequence using the ?* operator for easy iteration.
  • Maps: We build a map with mixed data types (including a nested array), access entries by key, iterate over all key-value pairs using map-specific functions, and drill into nested structures.

Expected Output:

XSLT Version: 3.0
Vendor: Saxonica
XPath Version: 3.1

=== Array Examples ===
First fruit: Apple
All fruits: Apple, Banana, Cherry

=== Map Examples ===
User name: Alice
User age: 30
All map entries:
name: Alice
age: 30
hobbies: Reading Hiking
User's first hobby: Reading

Common Pitfalls to Avoid:

  • Trailing spaces in system properties: Your original code had extra spaces in property names (like 'xsl:version ')—these will return empty values, so remove them.
  • Array indexing: XPath arrays start at 0, not 1. Using $array(1) will get the second element, not the first.
  • Map namespace: When using functions like map:entry-key(), you must declare the map namespace in your stylesheet (as shown in the example).
  • Map creation syntax: Use map { 'key': value } to define a map—don't confuse it with the map() function, which serves a different purpose.
  • Type clarity: Explicitly declaring variable types (like as="array(xs:string)") helps catch errors early and makes your code more readable.

This should run without issues in Saxon HE 9.8—let me know if you hit any snags!

内容的提问来源于stack exchange,提问作者DaveG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:44:58