2017-11-21 6 views
0

私はこの自由形式のxmlを持っています(dtdはありません、ダミーの名前空間の場合はmystuff)。 問題は私のselect文が要素を見つけられないことです。これはxpathの問題ですか?名前空間? xsltproc使用フリーフォームxmlのxslと一致する要素のテキスト

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

    <xsl:template match="/"> 
    <newdoc> 
    <heading> 
     <xsl:text>hey</xsl:text> 
     <xsl:value-of select='/document/Chapter/arg' /> 
     <xsl:text>hey</xsl:text> 
    </heading> 
    </newdoc> 
    </xsl:template> 

</xsl:stylesheet> 

その結果、::このXSLTで

<document xmlns="http://mystuff.org"> 
    <Chapter> 
    <arg name="title">title is here</arg> 
    </Chapter> 
</document> 

<?xml version="1.0"?> 
<newdoc xmlns="http://mystuff.org"> 
    <heading>heyhey</heading> 
</newdoc> 

答えて

1

になりすべてのxpath式が空の名前空間に一致する前に*:を置くことができます。

<xsl:template match="*:document"> 
    <newdoc> 
     <heading> 
      <xsl:text>hey</xsl:text> 
      <xsl:value-of select="/*:document/*:Chapter/*:arg"/> 
      <xsl:text>hey</xsl:text> 
     </heading> 
    </newdoc> 
</xsl:template> 

また、XSLT 2.0を使用できる場合は、xsl:stylesheetxpath-default-namespace="http://mystuff.org"を使用できます。

+0

ありがとうございます。私はDocBookの処理に慣れており、名前空間を持たなければならないと考えました。ちょうど自分のために物事をより困難にする。 :-) – Tim

2

設定し

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:p="http://mystuff.org"> 

と使用名前空間接頭辞

あなたがダミーの名前空間宣言を維持したい場合は

<newdoc> 
    <heading>heytitle is herehey</heading> 
</newdoc> 

 <xsl:value-of select='/p:document/p:Chapter/p:arg' /> 

それはあなたがダミーの名前空間宣言を省略した場合、それは(仮に)期待される出力を生成する必要があり

<heading>heytitle is herehey</heading> 
+0

エレガントな、私はそれを考えていたはずです! –

関連する問題