2012-03-28 15 views
2

XSLTを使用してHTMLファイルの属性を取り除くことを検討しています。 HTMLファイルには、次のようになります。それがコンセプトの証明であると私はそれぞれのdivのための新しいタグでに探していますXSLT書式設定HTML入力

<html> 
    <head> 
     <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" /> 
     <title>CB Comfy Bike</title> 
     <meta name="atg:string,index:$repositoryId" content="prod10005" /> 
     <meta name="atg:date:creationDate" content="955050507" /> 
     <meta name="atg:date:startDate" content="978325200" /> 
     <meta name="atg:date:endDate" content="1009861200" /> 
     <meta name="atg:string:$url" 
      content="atgrep:/ProductCatalog/frame-product/prod10005?locale=en_US" /> 
     <meta name="atg:string,index:$baseUrl" 
      content="atgrep:/ProductCatalog/frame-product/prod10005" /> 
     <meta name="atg:string:$repository.repositoryName" content="ProductCatalog" /> 
     <meta name="atg:string:$itemDescriptor.itemDescriptorName" content="frame-product" /> 
     <meta name="atg:string:childSKUs.$repositoryId" content="sku20007" /> 
     <meta name="atg:string:childSKUs.$itemDescriptor.itemDescriptorName" content="bike-sku" /> 
     <meta name="atg:date:childSKUs.creationDate" content="955068027" /> 
     <meta name="atg:float:childSKUs.listPrice" content="400.0" /> 
     <meta name="atg:float:childSKUs.salePrice" content="300.0" /> 
     <meta name="atg:boolean:childSKUs.onSale" content="false" /> 
     <meta name="atg:string:parentCategory.$repositoryId" content="cat55551" /> 
     <meta name="atg:date:parentCategory.creationDate" content="956950321" /> 
     <meta name="atg:string,docset:ancestorCategories.$repositoryId" content="cat10002" /> 
     <meta name="atg:string,docset:ancestorCategories.$repositoryId" content="cat10003" /> 
     <meta name="atg:string,docset:ancestorCategories.$repositoryId" content="cat55551" /> 
    </head> 
    <body> 
     <div class="atg:role:displayName" id="0"> CB Comfy Bike </div> 
     <div class="atg:role:longDescription" id="1"> This bike is just right, whether you are a 
      commuter or want to explore the fire roads. The plush front suspension will smooth out 
      the roughest bumps and the big disc brakes provide extra stopping power for those big 
      downhills. </div> 
     <div class="atg:role:keywords" id="2"> mountain_bike comfort_bike </div> 
     <div class="atg:role:childSKUs.displayName" id="3"> CB Comfy Bike Medium </div> 
     <div class="atg:role:childSKUs.listPrice" id="4"> 400.0 </div> 
     <div class="atg:role:childSKUs.description" id="5"> Medium </div> 
     <div class="atg:role:parentCategory.displayName" id="6"> Mountain Bikes </div> 
    </body> 
</html> 

、私は、まだconvensions命名に焦点を当てていません。しかし、どのようにdivタグを区別するのか分からない。私がこれまで持っているこのXSLTは、次のとおりです。

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

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

    <xsl:template match="head"/> 

    <xsl:template match="body"> 
     <xsl:copy> 
      <xsl:value-of select="div/text()"/> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

<?xml version="1.0" encoding="utf-8"?> 
<html> 
    <body> CB Comfy Bike </body> 
</html> 
私はこの

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <tag1>CB Comfy Bike</tag1> 
    <tag2>This bike is just right, whether you are a 
     commuter or want to explore the fire roads. The plush front suspension will smooth out 
     the roughest bumps and the big disc brakes provide extra stopping power for those big 
     downhills.</tag2> 
    <tag3>mountain_bike comfort_bike</tag3> 
    <tag4>CB Comfy Bike Medium</tag4> 
    <tag5>400.0</tag5> 
    <tag6>Medium</tag6> 
    <tag7>Mountain Bikes</tag7> 
</root> 

のようなものの中に私が間に差別されているトラブルの入力を有効にする方法を

をDivタグ。

+1

一部の(すべて?)XSLTプロセッサで任意のHTMLを処理する際に問題が発生することがあります。心に浮かぶ主なものは、XML仕様で多くの名前付きエンティティがないことです。 私が間違っていると誰かが私を修正できますか? – Sprague

答えて

0

この短いと単純な変換する:

<html> 
     <head> 
      <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" /> 
      <title>CB Comfy Bike</title> 
      <meta name="atg:string,index:$repositoryId" content="prod10005" /> 
      <meta name="atg:date:creationDate" content="955050507" /> 
      <meta name="atg:date:startDate" content="978325200" /> 
      <meta name="atg:date:endDate" content="1009861200" /> 
      <meta name="atg:string:$url" 
       content="atgrep:/ProductCatalog/frame-product/prod10005?locale=en_US" /> 
      <meta name="atg:string,index:$baseUrl" 
       content="atgrep:/ProductCatalog/frame-product/prod10005" /> 
      <meta name="atg:string:$repository.repositoryName" content="ProductCatalog" /> 
      <meta name="atg:string:$itemDescriptor.itemDescriptorName" content="frame-product" /> 
      <meta name="atg:string:childSKUs.$repositoryId" content="sku20007" /> 
      <meta name="atg:string:childSKUs.$itemDescriptor.itemDescriptorName" content="bike-sku" /> 
      <meta name="atg:date:childSKUs.creationDate" content="955068027" /> 
      <meta name="atg:float:childSKUs.listPrice" content="400.0" /> 
      <meta name="atg:float:childSKUs.salePrice" content="300.0" /> 
      <meta name="atg:boolean:childSKUs.onSale" content="false" /> 
      <meta name="atg:string:parentCategory.$repositoryId" content="cat55551" /> 
      <meta name="atg:date:parentCategory.creationDate" content="956950321" /> 
      <meta name="atg:string,docset:ancestorCategories.$repositoryId" content="cat10002" /> 
      <meta name="atg:string,docset:ancestorCategories.$repositoryId" content="cat10003" /> 
      <meta name="atg:string,docset:ancestorCategories.$repositoryId" content="cat55551" /> 
     </head> 
     <body> 
      <div class="atg:role:displayName" id="0"> CB Comfy Bike </div> 
      <div class="atg:role:longDescription" id="1"> This bike is just right, whether you are a 
       commuter or want to explore the fire roads. The plush front suspension will smooth out 
       the roughest bumps and the big disc brakes provide extra stopping power for those big 
       downhills. </div> 
      <div class="atg:role:keywords" id="2"> mountain_bike comfort_bike </div> 
      <div class="atg:role:childSKUs.displayName" id="3"> CB Comfy Bike Medium </div> 
      <div class="atg:role:childSKUs.listPrice" id="4"> 400.0 </div> 
      <div class="atg:role:childSKUs.description" id="5"> Medium </div> 
      <div class="atg:role:parentCategory.displayName" id="6"> Mountain Bikes </div> 
     </body> 
</html> 

:提供されるXML文書に適用

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

<xsl:template match="/*"> 
    <root> 
    <xsl:apply-templates select="body/div"/> 
    </root> 
</xsl:template> 

<xsl:template match="div"> 
    <xsl:element name="tag{position()}"> 
    <xsl:value-of select="."/> 
    </xsl:element> 
</xsl:template> 
</xsl:stylesheet> 

は、 SULT

<root> 
    <tag1> CB Comfy Bike </tag1> 
    <tag2> This bike is just right, whether you are a 
       commuter or want to explore the fire roads. The plush front suspension will smooth out 
       the roughest bumps and the big disc brakes provide extra stopping power for those big 
       downhills. </tag2> 
    <tag3> mountain_bike comfort_bike </tag3> 
    <tag4> CB Comfy Bike Medium </tag4> 
    <tag5> 400.0 </tag5> 
    <tag6> Medium </tag6> 
    <tag7> Mountain Bikes </tag7> 
</root> 

説明

  1. テンプレートを適切に使用。

  2. position()の使用。

  3. (属性値テンプレート)の使用。

+0

には、このタグ「

mountain_bike comfort_bike
」だけを取り出すことができる位置を忘れる方法があります。私はクラスまたはIDを使用して、私が望むタグだけを選ぶことができますか?私は ''のようなビットです。私はこれがうまくいかないことを知っていますが、それはうまくいけば何を求めているかについてのアイデアを与えるでしょう。 –

+0

@liveek:もちろん、 '/ */body/div [@id = 2]/text()'のようなXPath式を使って、そのようなテキストノードを選択することができます。したがって、テンプレートのマッチパターン(現在 'div'にマッチしている)を' div [@id = 2] 'に置き換えることができます。あなたの問題は、正確にあなたが何を望んでいるのかわからないということです。これは質問に表示され、コメントで明確にする必要があります。現在の質問は完全に答えられます。質問を編集してより具体的にするか、新しい質問をすることができます。 –

0

ほぼ所望の出力)

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

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

    <xsl:template match="head"/> 

    <xsl:template match="body"> 
    <xsl:for-each select="div"> 
     <xsl:element name="{concat('tag', position())}"> 
     <xsl:value-of select="./text()"/> 
     </xsl:element> 
    </xsl:for-each> 
    </xsl:template> 

</xsl:stylesheet> 
+0

には、このタグ「

mountain_bike comfort_bike
」だけを引くことができる位置を忘れる方法があります。私はクラスまたはIDを使用して、私が望むタグだけを選ぶことができますか?私は ''のようなビットです。私はこれがうまくいかないことを知っていますが、それはうまくいけば何を求めているかについてのアイデアを与えるでしょう。 –

0

私はティムールに似た何かをしたいが、私はのために、それぞれ使用することはありません。

    :私は

    <?xml version="1.0" encoding="UTF-8"?> 
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
        <xsl:output method="xml" indent="yes"/> 
        <xsl:strip-space elements="*"/> 
    
        <xsl:template match="@*|node()"> 
         <xsl:copy> 
          <xsl:apply-templates select="@*|node()"/> 
         </xsl:copy> 
        </xsl:template> 
    
        <xsl:template match="html"> 
         <xsl:apply-templates select="body"/> 
        </xsl:template> 
    
        <xsl:template match="body"> 
         <root> 
          <xsl:apply-templates select="div"/> 
         </root> 
        </xsl:template> 
    
        <xsl:template match="div"> 
         <xsl:element name="{concat('tag', position())}"> 
          <xsl:value-of select="."/> 
         </xsl:element> 
        </xsl:template> 
    
    </xsl:stylesheet> 
    

    が所望の出力

    <?xml version="1.0" encoding="UTF-8"?> 
    <root> 
        <tag1> CB Comfy Bike </tag1> 
        <tag2> This bike is just right, whether you are a 
         commuter or want to explore the fire roads. The plush front suspension will smooth out 
         the roughest bumps and the big disc brakes provide extra stopping power for those big 
         downhills. </tag2> 
        <tag3> mountain_bike comfort_bike </tag3> 
        <tag4> CB Comfy Bike Medium </tag4> 
        <tag5> 400.0 </tag5> 
        <tag6> Medium </tag6> 
        <tag7> Mountain Bikes </tag7> 
    </root> 
    

    の使用を生産、供給されたXMLに次を適用する身体

    内のdivのを反復処理するためにテンプレートを使用したいです

  1. ID変換
  2. 属性値temプレート
  3. が適用され、テンプレートに対して繰り返し子ノード
+0

は、このタグ「

mountain_bike comfort_bike
」だけを引っ張ることができる位置を忘れる方法です。私はクラスまたはIDを使用して、私が望むタグだけを選ぶことができますか?私は ''のようなビットです。私はこれがうまくいかないことを知っていますが、それはうまくいけば何を求めているかについてのアイデアを与えるでしょう。 –