【问题标题】:Dynamic xsl:value-of using data from a parameter动态 xsl:value-of 使用来自参数的数据
【发布时间】:2013-06-26 14:53:19
【问题描述】:

我正在尝试使用类似过滤器从 XSL 中的 XML 参数中列出的字段从 XML 中获取数据: 参数是这样的xml:

<Catalog name="Catalog1">
    <Fields>
        <Field>ID</Field>
        <Field>Name</Field>
        <Field>Description</Field>
    </Fields>
</Catalog>

所以我需要从另一个像这样的 XML 中检索这些元素:

<?xml version="1.0" encoding="UTF-8"?>
<Nodes>
    <Node>
        <Data>
            <Attribute name="ID">Desktop</Attribute>
            <Attribute name="Parent">administrator</Attribute>
            <Attribute name="Name">Desktop</Attribute>
            <Attribute name="Description">Desktop folder</Attribute>
        </Data>
        <Relationship>
            <RelatedNodes>
                <Node>
                    <Data>
                        <Attribute name="ID">administrator</Attribute>
                        <Attribute name="Parent"></Attribute>
                        <Attribute name="Name">administrator</Attribute>
                        <Attribute name="Description">administrator folder</Attribute>
                    </Data>
                </Node>
            </RelatedNodes>
        </Relationship>
    </Node>
</Nodes>

到目前为止,我有一个像这样的 XSL 模板:

<?xml version="1.0"?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' >
    <xsl:output method='text'  encoding="UTF-8"/>
    <xsl:param name="Fields"> <!--type="xml"--></xsl:param>
    <xsl:template match="/">
        {
        <xsl:apply-templates select="//Data[Attribute[@name='Parent']='' ]/parent::Node" />
        }
    </xsl:template>

    <xsl:template match="Node" >
        "children":[
            {
                <xsl:for-each select="$Fields/Catalog/Fields/Field">
                    <xsl:variable name="AttributeName" select="." />
                    "<xsl:value-of select="$AttributeName"/>": " <xsl:value-of select="Data/Attribute[@name=$AttributeName]" /> ",
                </xsl:for-each>
                <xsl:apply-templates select="ancestor::Node[1]"/>
            }
        ]
    </xsl:template>
</xsl:stylesheet>

但是不起作用,xpath 没有获取值,我认为这是因为我必须在 xpath 的过滤器中设置“'”字符,这就是我使用 concat 但 concat 只是返回给我的原因xpath 作为字符串,有什么想法吗?,结果应该是这样的:

{
    "children":[
        {
            "ID": "1",
            "Name": "Name1",
            "Description: "Desc1",
        }
    ]
}

谢谢!

【问题讨论】:

    标签: xml xslt dynamic xpath value-of


    【解决方案1】:

    因为你只展示了你的问题的一部分,我只能猜测。 似乎您的 XML 中有一些数据/属性条目。要访问它们,您不应像以前那样在选择中使用 concat。

    尝试类似:

        <xsl:for-each select="$Fields/Catalog/Fields/Field">
            <xsl:variable name="name" select="." />
            "<xsl:value-of select="."/>": " <xsl:value-of select="//Data/Attribute[@name= $name]" /> ",
        </xsl:for-each>
    

    【讨论】:

      【解决方案2】:

      如果您的动态路径仅包含元素名称,那么您可以使用

      *[name()=$path]
      

      如果它们是更通用的路径,那么您需要一个函数来评估作为字符串动态提供的 XPath 表达式。在 XSLT 3.0 之前的标准中没有这样的函数,但是许多 XSLT 处理器都有一个合适的扩展函数,通常称为 xx:evaluate(),其中 xx 是一些供应商前缀。

      【讨论】:

      • 您能进一步解释一下您的解决方案吗?
      • 除非你告诉我你不明白其中的哪一部分。
      猜你喜欢
      • 2015-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多