0

私はXMLを持っています

<?xml version="1.0" encoding="UTF-8"?>
<icestats>
    <stats_connections>0</stats_connections>
    <source mount="/live">
        <bitrate>Some data</bitrate>
        <server_description>This is what I want to return</server_description>
    </source>
</icestats>

そして私はXSLを持っています

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:copy-of select="/icestats/source mount="/live"/server_description/node()" />
    </xsl:template>
</xsl:stylesheet>

出力が欲しい

This is what I want to return

ソースから二重引用符、スペース、スラッシュを削除すると機能しますが、他の投稿で提案されている方法を使用しても、非標準文字をうまくエスケープできませんでした。


わかりやすくするために、以下はLego Stormtrooprのおかげで解決策です

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:copy-of select="/icestats/source[@mount='/live']/server_description/node()" />
    </xsl:template>
</xsl:stylesheet>
4

3 に答える 3

1

プロセッサが探している出力を生成する前に、解決する必要がある問題がいくつかあります。

1) XML 入力は整形式にする必要があります。要素の終了タグには、開始タグで指定された属性をsource含めないでください。mount

<source mount="/live">
   ...
</source>

2)xsl:copy-of要素の XPath を有効にする必要があります。XPath 式の構文は、(幸いなことに) XML 要素および属性の構文とは異なります。source角括弧を使用する必要があることを除いて、一致する要素を指定するには、属性値を述語することによって行います。

/icestats/source[@mount="/live"]/server_description

この XPath 式を XSLTselectステートメントで使用するには、属性値全体selectを 1 つのタイプの引用符で囲み、属性値内で別のタイプの引用符を使用する必要があります。次に例を示します。

<xsl:value-of select="/icestats/source[@mount='/live']/server_description" />

この入力で

<?xml version="1.0" encoding="UTF-8"?>
<icestats>
    <stats_connections>0</stats_connections>
    <source mount="/live">
        <bitrate>Some data</bitrate>
        <server_description>This is what I want to return</server_description>
    </source>
</icestats>

そしてこのスタイルシート

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text"/>
    <xsl:template match="/">
        <xsl:value-of select="/icestats/source[@mount='/live']/server_description" />
    </xsl:template>
</xsl:stylesheet>

xsltproc と saxon から次のテキスト行を取得します。

This is what I want to return

このxsl:value-of要素は、要素 (ここでは、その 1 つのテキスト ノード) の文字列値を返します。実際にserver_description要素が必要な場合は、 を使用xsl:copy-ofしてすべてのもの、タグ、およびすべてを取得できます。(更新xsl:outputも必要です。)

于 2013-07-22T23:58:29.277 に答える