2008-09-16 6 views
3

私はネームスペースを特定できないサードパーティのフィードを受け取りました。現在、XSLTでlocal-name()関数を使用して要素値を取得する必要があります。しかし、そのような要素から属性を取得する必要があり、名前空間が不明な場合(つまり、local-name()関数の必要性)、これを行う方法がわかりません。不明な名前空間のXSLTを使用する場合、どのように属性値を取得しますか?

N.B. IここではXSLT

を処理するために、.NET 2.0を使用してXMLのサンプルです午前:私は$ CurrentCategory変数を交換しようとしている

<?xml version="1.0" encoding="UTF-8"?> 
<feed xmlns="http://www.w3.org/2005/Atom"> 
    <id>some id</id> 
    <title>some title</title> 
    <updated>2008-09-11T15:53:31+01:00</updated> 
    <link rel="self" href="http://www.somefeedurl.co.uk" /> 
    <author> 
     <name>some author</name> 
     <uri>http://someuri.co.uk</uri> 
    </author> 
    <generator uri="http://aardvarkmedia.co.uk/">AardvarkMedia script</generator> 
    <entry> 
     <id>http://soemaddress.co.uk/branded3/80406</id> 
     <title type="html">My Ttile</title> 
     <link rel="alternate" href="http://www.someurl.co.uk" /> 
     <updated>2008-02-13T00:00:00+01:00</updated> 
     <published>2002-09-11T14:16:20+01:00</published> 
     <category term="mycategorytext" label="restaurant">Test</category> 
     <content type="xhtml"> 
     <div xmlns="http://www.w3.org/1999/xhtml"> 
      <div class="vcard"> 
       <p class="fn org">some title</p> 
       <p class="adr"> 
        <abbr class="type" title="POSTAL" /> 
        <span class="street-address">54 Some Street</span> 
        , 
        <span class="locality" /> 
        , 
        <span class="country-name">UK</span> 
       </p> 
       <p class="tel"> 
        <span class="value"></span> 
       </p> 
       <div class="geo"> 
        <span class="latitude">51.99999</span> 
        , 
        <span class="longitude">-0.123456</span> 
       </div> 
       <p class="note"> 
        <span class="type">Review</span> 
        <span class="value">Some content</span> 
       </p> 
       <p class="note"> 
        <span class="type">Overall rating</span> 
        <span class="value">8</span> 
       </p> 
      </div> 
     </div> 
     </content> 
     <category term="cuisine-54" label="Spanish" /> 
     <Point xmlns="http://www.w3.org/2003/01/geo/wgs84_pos#"> 
     <lat>51.123456789</lat> 
     <long>-0.11111111</long> 
     </Point> 
    </entry> 
</feed> 

これは、XSLT

<?xml version="1.0" encoding="UTF-8" ?> 

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:wgs="http://www.w3.org/2003/01/geo/wgs84_pos#" exclude-result-prefixes="atom wgs"> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:key name="uniqueVenuesKey" match="entry" use="id"/> 
    <xsl:key name="uniqueCategoriesKey" match="entry" use="category/@term"/> 

    <xsl:template match="/"> 
    <locations> 
     <!-- Get all unique venues --> 
     <xsl:for-each select="/*[local-name()='feed']/*[local-name()='entry']"> 
     <xsl:variable name="CurrentVenueKey" select="*[local-name()='id']" ></xsl:variable> 
     <xsl:variable name="CurrentVenueName" select="*[local-name()='title']" ></xsl:variable> 
     <xsl:variable name="CurrentVenueAddress1" select="*[local-name()='content']/*[local-name()='div']/*[local-name()='div']/*[local-name()='p'][@class='adr']/*[local-name()='span'][@class='street-address']" ></xsl:variable> 
     <xsl:variable name="CurrentVenueCity" select="*[local-name()='content']/*[local-name()='div']/*[local-name()='div']/*[local-name()='p'][@class='adr']/*[local-name()='span'][@class='locality']" ></xsl:variable> 
     <xsl:variable name="CurrentVenuePostcode" select="*[local-name()='postcode']" ></xsl:variable> 
     <xsl:variable name="CurrentVenueTelephone" select="*[local-name()='telephone']" ></xsl:variable> 
     <xsl:variable name="CurrentVenueLat" select="*[local-name()='Point']/*[local-name()='lat']" ></xsl:variable> 
     <xsl:variable name="CurrentVenueLong" select="*[local-name()='Point']/*[local-name()='long']" ></xsl:variable> 
     <xsl:variable name="CurrentCategory" select="WHATDOIPUTHERE"></xsl:variable> 

      <location> 
       <locationName> 
       <xsl:value-of select = "$CurrentVenueName" /> 
       </locationName> 
       <category> 
       <xsl:value-of select = "$CurrentCategory" /> 
       </category> 
       <description> 
        <xsl:value-of select = "$CurrentVenueName" /> 
       </description> 
       <venueAddress> 
       <streetName> 
        <xsl:value-of select = "$CurrentVenueAddress1" /> 
       </streetName> 
       <town> 
        <xsl:value-of select = "$CurrentVenueCity" /> 
       </town> 
       <postcode> 
        <xsl:value-of select = "$CurrentVenuePostcode" /> 
       </postcode> 
       <wgs84_latitude> 
        <xsl:value-of select = "$CurrentVenueLat" /> 
       </wgs84_latitude> 
       <wgs84_longitude> 
        <xsl:value-of select = "$CurrentVenueLong" /> 
       </wgs84_longitude> 
       </venueAddress> 
       <venuePhone> 
       <phonenumber> 
        <xsl:value-of select = "$CurrentVenueTelephone" /> 
       </phonenumber> 
       </venuePhone> 
      </location> 
     </xsl:for-each> 
    </locations> 
    </xsl:template> 
</xsl:stylesheet> 

です適切なコードを表示するmycategorytext

+0

質問のタイトルを短くしてください。 –

+0

あまりにもあいまいではないと思って申し訳ありません。 –

答えて

12

ここにはXSLTエディタがありませんが、

*[local-name()='category']/@*[local-name()='term'] 
+0

は完璧にお礼を言います。 –

+0

私はMSがデフォルトの名前空間処理をオプションとして統合したいと思っていますが、これは非常に便利でした..... – Adrian

0

local-name()を使用する必要があるのは本当にわかりませんが、xsltプロセッサの言語に関する情報を共有している場合は、理解することができます。私はあなたが助けを望んでいる二つのものが接頭辞なしで先頭にxmlns宣言されている

<xsl:stylesheet xmlns="http://www.w3.org/2005/Atom" ..> 

<xsl:template match="feed"> 
    <xsl:apply-templates /> 
</xsl:template> 

<xsl:template match="entry"> 
    ... 
    <xsl:variable name="current-category" select="category/@term" /> 
    ... 
</xsl:template> 

:私のような何かを行うことができるはず、このB/Cと言います。これは、名前空間接頭辞を使用する必要がないようにデフォルトの名前空間を設定します。同様に、do 'xmlns:a = "http://www.w3.org/2005/Atom"'を呼び出し、次に 'select = "a:feed"'を実行することもできます。もう1つ注目すべきは、属性を選択する '@term'を使うことです。属性 '@ *'に一致させたい場合は、要素の場合と同じように動作します。

また、プロセッサによっては、便利なツールが他にもあるかもしれませんので、少し詳しい情報があれば役立ちます。また、XSL mailing listは別の有用なリソースかもしれません。

2

http://www.w3.org/TR/2006/REC-xml-names-20060816/#scoping-defaulting

によると、「デフォルトの名前空間宣言は、属性名に直接適用されません。接頭辞属性の解釈は、それらが表示されている要素によって決定されます。」

これは、属性が名前空間にないことを意味します。単に "@term"を使用してください。

この問題を解決するためにlocal-name()を使用する必要はありません。 これを処理する従来の方法は、XSLT内の原子名前空間のプレフィックスを宣言し、それをxpathクエリで使用することです。

あなたのスタイルシート要素(xmlns:atom = "http://www.w3.org/2005/Atom")に既にこの宣言があります。残っているのはそれを使用することだけです。

私はすでに説明したように、属性はデフォルトの名前空間に影響されないので、あなたのコードは(あなたが「のxmlns:XHTML = 『http://www.w3.org/1999/xhtml』」を追加したと仮定して):次のようになります

 <xsl:for-each select="/atom:feed/atom:entry"> 
     <xsl:variable name="CurrentVenueKey" select="atom:id" /> 
     <xsl:variable name="CurrentVenueName" select="atom:title" /> 
     <xsl:variable name="CurrentVenueAddress1" 
      select="atom:content/xhtml:div/xhtml:div/xhtml:p[@class='adr']/xhtml:span[@class='street-address']" /> 
     <xsl:variable name="CurrentVenueCity" 
      select="atom:content/xhtml:div/xhtml:div'/xhtml:p[@class='adr']/xhtml:span[@class='locality'] /> 
... 
     <xsl:variable name="CurrentCategory" select="atom:category/@term" /> 

..... 

あなたが本当に変換しているXMLの構造を知らない場合、local-name()は非常に便利ですが、この場合、あなたが期待しているもの以外のものを受け取った場合は、 。

関連する問題