2



属性が「running-head」と等しい場合は 、XMLを変換alt-titleし 、という名前の各要素の名前を変更します。Running_Headalt-title-type


したがって、以下のコードは正常に機能している行<xsl:when test="starts-with(@alt-title-type, 'running-head')">を使用しています。ただし、これを次のいずれかに変更すると、次のようになります。

  • <xsl:when test="ends-with(@alt-title-type, 'running-head')">
  • <xsl:when test="matches(@alt-title-type, 'running-head')">

...このエラーがスローされます:

エラー:XSLTProcessor :: transformToXml()[xsltprocessor.transformtoxml]:xmlXPathCompiledEval:スタックに2つのオブジェクトが残っています。

したがって、関数starts-withは機能しているように見えますが、機能ends-withしてmatchesいません。


これが私のXSLで、を使用してstarts-with、正しく機能しているようです。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" indent="yes" method="xml" />

    <!-- Running_Head -->
    <xsl:template match="@*|node()">
        <xsl:choose>

            <xsl:when test="starts-with(@alt-title-type, 'running-head')">
                <xsl:element name="Running_Head">
                    <xsl:apply-templates select="@*|node()"/>
                </xsl:element>
            </xsl:when>

            <xsl:otherwise>
                <xsl:copy>
                    <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
            </xsl:otherwise>

        </xsl:choose>
    </xsl:template> <!-- end of Running_Head -->

</xsl:stylesheet>

...そしてこれが変換されるXMLです:

<root-node>
    <alt-title alt-title-type="running-head">
        This is working
    </alt-title>
    <alt-title alt-title-type="asdfng-head">
        asdfasdf
    </alt-title>
    <alt-title>
        asdfasdf
    </alt-title>
    <alt-title alt-title-type="running-head">
        This is also working
    </alt-title>
</root-node>


私はこれをhttp://xslt.online-toolz.com/tools/xslt-transformation.phphttp://www.xsltcake.com/でテストしています。

4

3 に答える 3

3

XPath2.0のみにmatchesandends-with関数があります。

XPath 1.0では、次のends-withように記述する必要があります

$suffix = substring($target, string-length($target) - string-length($suffix) + 1)

正規表現機能はありませんが、多分

contains($ target、$ substring)

正規表現のメタ文字を使用していない場合は、

于 2012-07-29T21:07:01.087 に答える
3

他の人が指摘しているように、ほとんどのXPath 2.0関数(やなどmatches()ends-with()はXSLT1.0プロセッサではサポートされていません。

さらに、これらの関数は、現在の要件を実装する変換ではまったく必要ありません

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="alt-title[@alt-title-type='running-head']">
  <Running_Head>
    <xsl:apply-templates select="@*|node()"/>
  </Running_Head>
 </xsl:template>
</xsl:stylesheet>

この変換が提供されたXMLドキュメントに適用される場合

<root-node>
    <alt-title alt-title-type="running-head">
      This is working
  </alt-title>
    <alt-title alt-title-type="asdfng-head">
     asdfasdf
  </alt-title>
    <alt-title>
     asdfasdf
  </alt-title>
    <alt-title alt-title-type="running-head">
     This is also working
  </alt-title>
</root-node>

必要な正しいreaultが生成されます:

<root-node>
   <Running_Head alt-title-type="running-head">
      This is working
  </Running_Head>
   <alt-title alt-title-type="asdfng-head">
     asdfasdf
  </alt-title>
   <alt-title>
     asdfasdf
  </alt-title>
   <Running_Head alt-title-type="running-head">
     This is also working
  </Running_Head>
</root-node>

説明

テンプレートの適切な使用、一致パターン、およびIDルールのオーバーライド。

于 2012-07-29T22:32:26.377 に答える
0

テンプレートをXSLT1.0として宣言しますが、2.0関数ends-withとを使用していますmatches。XSL 2.0プロセッサを使用して(または不足している機能を回避して)、ドキュメントをXSLT2.0として宣言します。

発生するエラーは、これらのサービスで使用されるXSLプロセッサーの内部エラーであることに注意してください(未定義の関数を正しく処理していないようです)。

于 2012-07-29T21:07:45.587 に答える