2012-04-21 55 views
0

打招呼,好日子親愛XML的朋友,在XSLT處理器添加新的XML元素 - 一個新手的事情

我是XML新手,所以不要和我一起承擔的問題...我目前製備一些測試使用XSLT處理器 -

新:看到更新我添加在這一初始張貼 - 一種解決方案II複製邏輯爲標籤名 - >我添加了一個例子在初始發佈結束時。 PLZ還是讓我知道你的IDAS ... thgx

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

    <xsl:output method="text" encoding="UTF-8"/> 
    <xsl:template match="/"> 

     <xsl:for-each select="/osm/node/tag[@k='amenity' and @v='restaurant']"> 
      <xsl:value-of select="../@id"/> 
      <xsl:text>&#x09;</xsl:text> 
      <xsl:value-of select="../@lat"/> 
      <xsl:text>&#x09;</xsl:text> 
      <xsl:value-of select="../@lon"/> 
      <xsl:text>&#x09;</xsl:text> 
      <xsl:for-each select="../tag[@k='name']"> 
      <xsl:value-of select="@v"/> 
      </xsl:for-each> 
      <xsl:text>&#x0A;</xsl:text> 
     </xsl:for-each> 
<xsl:for-each select="../tag[@k='website']"> 
    <xsl:value-of select="@v"/> 
    </xsl:for-each> 
    <xsl:text>&#x0A;</xsl:text> 
    </xsl:template> 
</xsl:stylesheet> 

注:此上述方案中沒有給出任何iadditional的相關信息 - 這樣的網站是未顯示。這裏有什麼不對?!

此處初始張貼開始。

我在終端運行此解析XML文檔

問:我想是擴大XSLT處理器使用以下標記

wheelchair 
website  
addr:country 
addr:street 
addr:city 
addr:housenumber 
  • 查看更多詳情在這裏的一個例子...:

我運行以下代碼:時間xsltproc的test3.xslt testdoc3.xml>餐館-004.csv

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

    <xsl:output method="text" encoding="UTF-8"/> 
    <xsl:template match="/"> 

     <xsl:for-each select="/osm/node/tag[@k='amenity' and @v='restaurant']"> 
      <xsl:value-of select="../@id"/> 
      <xsl:text>&#x09;</xsl:text> 
      <xsl:value-of select="../@lat"/> 
      <xsl:text>&#x09;</xsl:text> 
      <xsl:value-of select="../@lon"/> 
      <xsl:text>&#x09;</xsl:text> 
      <xsl:for-each select="../tag[@k='name']"> 
      <xsl:value-of select="@v"/> 
      </xsl:for-each> 
      <xsl:text>&#x0A;</xsl:text> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

....上此XML的文檔 - 這叫做testdoc3.xml

<?xml version='1.0' encoding='UTF-8'?> 
<osm version="0.6" generator="Osmosis SNAPSHOT-r26564"> 
    <node id="185612117" version="5" timestamp="2011-01-08T19:23:43Z" uid="290680" user="wheelmap_visitor" changeset="6906586" lat="48.8037523" lon="9.5248779"> 
    <tag k="addr:postcode" v="73614"/> 
    <tag k="wheelchair" v="yes"/> 
    <tag k="website" v="http://www.courage-restaurant.de/"/> 
    <tag k="addr:country" v="DE"/> 
    <tag k="name" v="Courage"/> 
    <tag k="amenity" v="restaurant"/> 
    <tag k="addr:street" v="Künkelinstraße"/> 
    <tag k="addr:city" v="Schorndorf"/> 
    <tag k="addr:housenumber" v="33"/> 
    </node> 
    <node id="252619189" version="5" timestamp="2011-01-08T19:21:43Z" uid="290680" user="wheelmap_visitor" changeset="6906586" lat="48.8067032" lon="9.5314986"> 
    <tag k="addr:postcode" v="73614"/> 
    <tag k="wheelchair" v="yes"/> 
    <tag k="website" v="www.kesselhaus-schorndorf.de"/> 
    <tag k="addr:country" v="DE"/> 
    <tag k="name" v="Kesselhaus"/> 
    <tag k="amenity" v="restaurant"/> 
    <tag k="addr:street" v="Arnoldstraße"/> 
    <tag k="addr:city" v="Schorndorf"/> 
    <tag k="addr:housenumber" v="3"/> 
    </node> 
    <node id="319597380" version="1" timestamp="2008-12-17T21:13:15Z" changeset="444629" lat="48.8277913" lon="9.5477029"> 
    <tag k="name" v="Gasthaus zur Linde"/> 
    <tag k="amenity" v="restaurant"/> 
    </node> 
    <node id="319597382" version="1" timestamp="2008-12-17T21:13:15Z" changeset="444629" lat="48.8282523" lon="9.5503109"> 
    <tag k="name" v="China-Garden"/> 
    <tag k="amenity" v="restaurant"/> 
    </node> 
    <node id="339591124" version="2" timestamp="2011-09-22T17:33:36Z" uid="290680" user="wheelmap_visitor" changeset="9366746" lat="48.7888015" lon="9.5079525"> 
    <tag k="cuisine" v="italian"/> 
    <tag k="wheelchair" v="limited"/> 
    <tag k="name" v="Pizzeria da Rocco"/> 
    <tag k="amenity" v="restaurant"/> 
    </node> 
    <node id="392682646" version="3" timestamp="2010-05-11T19:00:20Z" uid="12973" user="MattGPS" changeset="4671372" lat="48.8315734" lon="9.5468864"> 
    <tag k="name" v="Gasthaus an der Wieslauf"/> 
    <tag k="amenity" v="restaurant"/> 
    </node> 
    <node id="454516720" version="4" timestamp="2010-12-11T15:30:21Z" uid="12982" user="Michael Schulze" changeset="6625571" lat="48.8031264" lon="9.5344371"> 
    <tag k="addr:postcode" v="73614"/> 
    <tag k="cuisine" v="regional"/> 
    <tag k="website" v="http://www.deutscheshaus-schorndorf.de/"/> 
    <tag k="addr:country" v="DE"/> 
    <tag k="name" v="Deutsches Haus"/> 
    <tag k="amenity" v="restaurant"/> 
    <tag k="addr:street" v="Sonnenscheinstraße"/> 
    <tag k="addr:city" v="Schorndorf"/> 
    <tag k="addr:housenumber" v="13"/> 
    </node> 
    <node id="572362430" version="3" timestamp="2010-07-04T06:59:50Z" uid="106522" user="neuntoeter" changeset="5130101" lat="48.807953" lon="9.5379673"> 
    <tag k="smoking" v="no"/> 
    <tag k="name" v="Club Kneipe"/> 
    <tag k="amenity" v="restaurant"/> 
    </node> 
    <node id="613988559" version="1" timestamp="2010-01-15T01:20:03Z" uid="24748" user="mabapla" changeset="3621550" lat="48.7991827" lon="9.551828"> 
    <tag k="name" v="Gaststätte Schützenhaus"/> 
    <tag k="amenity" v="restaurant"/> 
    </node> 
    <node id="859532091" version="1" timestamp="2010-08-13T23:05:14Z" uid="130472" user="fx99" changeset="5486736" lat="48.8062337" lon="9.5278548"> 
    <tag k="website" v="http://www.weinstube-buechsenmacher.de/"/> 
    <tag k="name" v="Weinstube zum Büchsenmacher"/> 
    <tag k="amenity" v="restaurant"/> 
    <tag k="addr:housename" v="5"/> 
    <tag k="addr:street" v="Höllgasse"/> 
    </node> 
    <node id="1625565012" version="1" timestamp="2012-02-09T18:00:04Z" uid="131968" user="changchun_1" changeset="10636279" lat="48.8151893" lon="9.5310692"> 
    <tag k="name" v="Gasthaus Sonne"/> 
    <tag k="amenity" v="restaurant"/> 
    </node> 
</osm> 

我回來這些結果 - 是:我想與上述標籤放大...

185612117 48.8037523 9.5248779 Courage 
252619189 48.8067032 9.5314986 Kesselhaus 
319597380 48.8277913 9.5477029 Gasthaus zur Linde 
319597382 48.8282523 9.5503109 China-Garden 
339591124 48.7888015 9.5079525 Pizzeria da Rocco 
392682646 48.8315734 9.5468864 Gasthaus an der Wieslauf 
454516720 48.8031264 9.5344371 Deutsches Haus 
572362430 48.807953 9.5379673 Club Kneipe 
613988559 48.7991827 9.551828 Gaststätte Schützenhaus 
631421882 48.8070643 9.5437351 Remstalstuben 
672817732 48.8045127 9.5254423 Hanti Alem 
677555759 48.8048108 9.5243952 Weinstube St. Urban 
677578941 48.8052132 9.5246951 Bei Domenico 
677578944 48.8055998 9.5261987 Little Saigon 

再次想要的標記,擴大XSLT的預處理器有以下的...

wheelchair 
website  
addr:country 
addr:street 
addr:city 
addr:housenumber 

THX的任何和所有幫助 - THX提前

greetigngs

更新 - 我做liek如此 - 在複製邏輯的標籤名 - 比如添加標籤的網站:

<xsl:for-each select="../tag[@k='website']"> 
    <xsl:value-of select="@v"/> 
    </xsl:for-each> 
    <xsl:text>&#x0A;</xsl:text> 
</xsl:for-each> 

你說什麼!?

+0

是否有任何理由,爲什麼你不只是複製邏輯'標籤[@ K = '名']'? – erikxiv 2012-04-21 15:07:37

+0

美好的一天。我是新來的xml - 但我想你是對的 - 我這樣做...... 的 – zero 2012-04-21 15:11:41

+0

以及我在初始發佈的開始時加入的**試用版**但正如我們所看到的 - 這不會回報想要的結果 - 標籤網站**不會被解析!? – zero 2012-04-21 15:38:06

回答

1

你不需要for-each在所有<tag />節點,只篩選出那些你需要:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output method="text" encoding="utf-8" /> 

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

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

    <xsl:template match="node[tag[@k='amenity' and @v='restaurant']]"> 
     <xsl:value-of select="./@id"/> 
     <xsl:text>&#x09;</xsl:text> 
     <xsl:value-of select="./@lat"/> 
     <xsl:text>&#x09;</xsl:text> 
     <xsl:value-of select="./@lon"/> 
     <xsl:text>&#x09;</xsl:text> 
     <xsl:value-of select="./tag[@k = 'name']/@v"/> 
     <xsl:text>&#x09;</xsl:text> 
     <xsl:value-of select="./tag[@k = 'wheelchair']/@v"/> 
     <xsl:text>&#x09;</xsl:text> 
     <xsl:value-of select="./tag[@k = 'website']/@v"/> 
     <xsl:text>&#x09;</xsl:text> 
     <xsl:value-of select="./tag[@k = 'addr:country']/@v"/> 
     <xsl:text>&#x09;</xsl:text> 
     <xsl:value-of select="./tag[@k = 'addr:street']/@v"/> 
     <xsl:text>&#x09;</xsl:text> 
     <xsl:value-of select="./tag[@k = 'addr:city']/@v"/> 
     <xsl:text>&#x09;</xsl:text> 
     <xsl:value-of select="./tag[@k = 'addr:housenumber']/@v"/> 
     <xsl:text>&#x0A;</xsl:text> 
    </xsl:template> 

    <!-- all non-restaurant nodes --> 
    <xsl:template match="node[tag[@k='amenity' and @v!='restaurant']]" /> 

</xsl:stylesheet> 
+0

你好親愛的filburt - 很多很多謝謝你幫助我很多 - 問候! – zero 2012-04-21 16:03:04