1

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",
        }
    ]
}

ありがとう!

4

2 に答える 2

0

動的パスが要素名だけで構成されている場合は、次を使用できます

*[name()=$path]

それらがより一般的なパスである場合は、文字列として動的に提供される XPath 式を評価する関数が必要です。XSLT 3.0 までは標準にそのような関数はありませんでしたが、多くの XSLT プロセッサには適切な拡張関数があり、通常は xx:evaluate() と呼ばれます。xx はベンダー プレフィックスです。

于 2013-06-26T16:56:53.630 に答える