2013-05-24 2 views
0

XML 파일을 Excel에서 구분할 수있는 형식으로 변환하는 데 XSLT를 사용하고 있습니다 (샘플 코드는 나중에 표시됨). 문제는 모든 URL 마지막에 추가 된 버전이 있다는 사실과 관련이XSLT를 사용하여 문자열 분할

+---------------+---------------+----------+ 
|URL   |Title   | Version | 
+---------------+---------------+----------+ 
|dogs_are_cool |Dogs are cool | May 2013 | 
+---------------+---------------+----------+ 

: Excel에서 열 경우 예를 들어, 구분 된 버전처럼 보일 수 있습니다. 앞의 예제를 사용하면 dogs_are_cool은 실제로 dogs_are_cool_may2013.html입니다.

그 추가 된 버전으로이 일을하고 싶습니다 : URL을 인쇄 할 때

  • 이 버전을 제거합니다.
  • 버전을 다시 포맷하고 인쇄하십시오.

나는이 작업을 수행하는 가장 좋은 방법을 추측하고있어 어떻게 든 밑줄에 URL을 분할하는 것입니다. 그런 다음 마지막 요소를 하나의 변수에 넣고 다른 요소를 순서대로 인쇄하십시오. 밑줄을 다시 삽입하십시오.

나는 그것에 대해 어떻게 해야할지 잘 모릅니다.

샘플 XML :

<contents Url="toc_animals_may2013.html" Title="Animals"> 
    <contents Url="toc_apes_may2013.html" Title="Apes"> 
     <contents Url="chimps_may2013.html" Title="Some Stuff About Chimps" /> 
    </contents> 
    <contents Url="toc_cats" Title="Cats"> 
     <contents Url="hairless_cats_may2013.html" Title="OMG Where Did the Hair Go?"/> 
     <contents Url="wild_cats_may2013.html" Title="These Things Frighten Me"/> 
    </contents> 
    <contents Url="toc_dogs_may2013.html" Title="Dogs"> 
     <contents Url="toc_snorty_dogs_may2013.html" Title="Snorty Dogs"> 
      <contents Url="boston_terriers_may2013.html" Title="Boston Terriers" /> 
      <contents Url="french_bull_dogs_may2013.html" Title="Frenchies" /> 
     </contents> 
    </contents> 
</contents> 

샘플 XSLT는 :

<?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" indent="no"/> 

    <!-- This variable sets the delimiter symbol that Excel will use to seperate the cells --> 
    <xsl:variable name="delimiter">@</xsl:variable> 

    <xsl:template match="contents"> 

     <!-- Prints the URL --> 
     <xsl:value-of select="@Url"/> 
     <xsl:copy-of select="$delimiter" /> 

     <!-- Prints the title --> 
     <xsl:apply-templates select="@Title"/> 
     <xsl:copy-of select="$delimiter" /> 

     <!-- I'd like to print the version here --> 
     <xsl:copy-of select="$delimiter" /> 

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

</xsl:stylesheet> 

답변

1

은 우리를 돕기 위해 몇 가지 더 많은 템플릿을 추가하고 우리는 XSLT 짐승을 만들 수 있지만, 트릭을 할 것 같다 ...

<?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" indent="no"/> 
    <!-- This variable sets the delimiter symbol that Excel will use to seperate the cells --> 
    <xsl:variable name="delimiter">@</xsl:variable> 

    <xsl:template match="contents"> 
    <!-- Prints the URL --> 
    <xsl:choose> 
     <xsl:when test="contains(@Url, '.')"> 
     <xsl:call-template name="substring-before-last"> 
      <xsl:with-param name="list" select="@Url"/> 
      <xsl:with-param name="delimiter" select="'_'"/> 
     </xsl:call-template>    
     </xsl:when> 
     <xsl:otherwise><xsl:value-of select="@Url"/></xsl:otherwise> 
    </xsl:choose> 
    <xsl:copy-of select="$delimiter"/> 

    <!-- Prints the title --> 
    <xsl:apply-templates select="@Title"/> 
    <xsl:copy-of select="$delimiter"/> 

    <!-- Now do all the tricks to format the version --> 
    <xsl:variable name="withExtension"> 
     <xsl:call-template name="substring-after-last"> 
     <xsl:with-param name="string" select="@Url"/> 
     <xsl:with-param name="delimiter" select="'_'"/> 
     </xsl:call-template> 
    </xsl:variable> 

    <xsl:variable name="withoutExtension"> 
     <xsl:call-template name="substring-before-last"> 
     <xsl:with-param name="list" select="$withExtension"/> 
     <xsl:with-param name="delimiter" select="'.'"/> 
     </xsl:call-template> 
    </xsl:variable> 

    <xsl:variable name="withoutSpace"> 
     <xsl:value-of select="concat(translate(substring($withoutExtension, 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), substring($withoutExtension, 2))"/> 
    </xsl:variable> 

    <xsl:variable name="year"> 
     <xsl:value-of select="translate($withoutSpace,translate($withoutSpace, '', ''), '')"/> 
    </xsl:variable> 

    <xsl:value-of select="concat(substring-before($withoutSpace, $year), ' ', $year)"/> 
    <xsl:copy-of select="$delimiter"/> 
    </xsl:template> 

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

    <xsl:template name="substring-before-last"> 
    <xsl:param name="list"/> 
    <xsl:param name="delimiter"/> 
    <xsl:choose> 
     <xsl:when test="contains($list, $delimiter)"> 
     <xsl:value-of select="substring-before($list,$delimiter)"/> 
     <xsl:choose> 
      <xsl:when test="contains(substring-after($list,$delimiter),$delimiter)"> 
      <xsl:value-of select="$delimiter"/> 
      </xsl:when> 
     </xsl:choose> 
     <xsl:call-template name="substring-before-last"> 
      <xsl:with-param name="list" select="substring-after($list,$delimiter)"/> 
      <xsl:with-param name="delimiter" select="$delimiter"/> 
     </xsl:call-template> 
     </xsl:when> 
    </xsl:choose> 
    </xsl:template> 

    <xsl:template name="substring-after-last"> 
    <xsl:param name="string"/> 
    <xsl:param name="delimiter"/> 
    <xsl:choose> 
     <xsl:when test="contains($string, $delimiter)"> 
     <xsl:call-template name="substring-after-last"> 
      <xsl:with-param name="string" select="substring-after($string, $delimiter)"/> 
      <xsl:with-param name="delimiter" select="$delimiter"/> 
     </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:value-of select="$string"/> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 

</xsl:stylesheet> 

출력 :

[email protected]@May [email protected][email protected]@May [email protected]@Some Stuff About [email protected] [email protected][email protected]@ @[email protected] Where Did the Hair [email protected] [email protected][email protected] Things Frighten [email protected] [email protected][email protected]@May [email protected][email protected] [email protected] [email protected][email protected] [email protected] [email protected][email protected]@May [email protected] 
+0

이 훌륭하게 작동합니다! 모든 도움을 주셔서 감사합니다! – mrthetooth

2

XSLT 2.0을 사용할 수 있으면 훨씬 간단 해집니다.

XML 입력

<contents Url="toc_animals_may2013.html" Title="Animals"> 
    <contents Url="toc_apes_may2013.html" Title="Apes"> 
     <contents Url="chimps_may2013.html" Title="Some Stuff About Chimps" /> 
    </contents> 
    <contents Url="toc_cats" Title="Cats"> 
     <contents Url="hairless_cats_may2013.html" Title="OMG Where Did the Hair Go?"/> 
     <contents Url="wild_cats_may2013.html" Title="These Things Frighten Me"/> 
    </contents> 
    <contents Url="toc_dogs_may2013.html" Title="Dogs"> 
     <contents Url="toc_snorty_dogs_may2013.html" Title="Snorty Dogs"> 
      <contents Url="boston_terriers_may2013.html" Title="Boston Terriers" /> 
      <contents Url="french_bull_dogs_may2013.html" Title="Frenchies" /> 
     </contents> 
    </contents> 
</contents> 

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:param name="delim" select="'@'"/> 

    <xsl:template match="contents"> 
     <xsl:variable name="urlTokens" select="tokenize(@Url,'_')"/> 
     <xsl:value-of select="$urlTokens[not(position() = last())]" separator="_"/> 
     <xsl:value-of select="$delim"/> 
     <xsl:value-of select="concat(@Title,$delim)"/> 
     <xsl:analyze-string select="$urlTokens[last()]" regex="([a-z])([a-z]+)([0-9]+)"> 
      <xsl:matching-substring> 
       <xsl:value-of select="concat(upper-case(regex-group(1)),regex-group(2),' ',regex-group(3))"/>    
      </xsl:matching-substring> 
     </xsl:analyze-string> 
     <xsl:text>&#xA;</xsl:text> 
     <xsl:apply-templates/> 
    </xsl:template> 

</xsl:stylesheet> 

출력

[email protected]@May 2013 
[email protected]@May 2013 
[email protected] Stuff About [email protected] 2013 
[email protected]@ 
[email protected] Where Did the Hair [email protected] 2013 
[email protected] Things Frighten [email protected] 2013 
[email protected]@May 2013 
[email protected] [email protected] 2013 
[email protected] [email protected] 2013 
[email protected]@May 2013 
+0

감사합니다. Daniel. 2.0 솔루션은 확실히 훨씬 간단하지만 Excel은 2.0을 좋아하지 않습니다. – mrthetooth