2012-03-28 4 views
2

XSLT를 사용하여 HTML 파일의 특성을 제거하려고합니다. HTML 파일은 다음과 같습니다 : 그것은 개념의 증거이기 때문에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 태그 사이의 differenciate 모르겠다. 내가 지금까지 가지고이 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> 

은 원했던 올바른 re 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

    <?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> 
    

    사용을 생산

  1. 신원 변환
  2. 속성 값 tem 판
  3. 이 적용 템플릿 반복 자식 노드
+0

은 위치를 잊어 버리는 방법입니다. 예를 들어이 태그 '

mountain_bike comfort_bike
'만 가져올 수 있습니다. 클래스 또는 ID를 사용하여 원하는 태그 만 선택할 수 있습니까? 나는''와 같은 비트이다. 나는 이것이 작동하지 않을 것을 알고있다. 그러나 그것은 희망을 갖고 무엇이 묻고 있는지에 관해 생각을 줄 것이다. –