簡體   English   中英

通過使用XSL將XML文件划分為名稱/值對

[英]Dividing an XML file to name-value pair through using XSL

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
   <book id="bk103">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
   </book>
   <book id="bk104">
      <author>Corets, Eva</author>
      <title>Oberon's Legacy</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2001-03-10</publish_date>
      <description>In post-apocalypse England, the mysterious 
      agent known only as Oberon helps to create a new life 
      for the inhabitants of London. Sequel to Maeve 
      Ascendant.</description>
   </book>
   <book id="bk105">
      <author>Corets, Eva</author>
      <title>The Sundered Grail</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2001-09-10</publish_date>
      <description>The two daughters of Maeve, half-sisters, 
      battle one another for control of England. Sequel to 
      Oberon's Legacy.</description>
   </book>
   <book id="bk106">
      <author>Randall, Cynthia</author>
      <title>Lover Birds</title>
      <genre>Romance</genre>
      <price>4.95</price>
      <publish_date>2000-09-02</publish_date>
      <description>When Carla meets Paul at an ornithology 
      conference, tempers fly as feathers get ruffled.</description>
   </book>
   <book id="bk107">
      <author>Thurman, Paula</author>
      <title>Splish Splash</title>
      <genre>Romance</genre>
      <price>4.95</price>
      <publish_date>2000-11-02</publish_date>
      <description>A deep sea diver finds true love twenty 
      thousand leagues beneath the sea.</description>
   </book>
   <book id="bk108">
      <author>Knorr, Stefan</author>
      <title>Creepy Crawlies</title>
      <genre>Horror</genre>
      <price>4.95</price>
      <publish_date>2000-12-06</publish_date>
      <description>An anthology of horror stories about roaches,
      centipedes, scorpions  and other insects.</description>
   </book>
   <book id="bk109">
      <author>Kress, Peter</author>
      <title>Paradox Lost</title>
      <genre>Science Fiction</genre>
      <price>6.95</price>
      <publish_date>2000-11-02</publish_date>
      <description>After an inadvertant trip through a Heisenberg
      Uncertainty Device, James Salway discovers the problems 
      of being quantum.</description>
   </book>
   <book id="bk110">
      <author>O'Brien, Tim</author>
      <title>Microsoft .NET: The Programming Bible</title>
      <genre>Computer</genre>
      <price>36.95</price>
      <publish_date>2000-12-09</publish_date>
      <description>Microsoft's .NET initiative is explored in 
      detail in this deep programmer's reference.</description>
   </book>
   <book id="bk111">
      <author>O'Brien, Tim</author>
      <title>MSXML3: A Comprehensive Guide</title>
      <genre>Computer</genre>
      <price>36.95</price>
      <publish_date>2000-12-01</publish_date>
      <description>The Microsoft MSXML3 parser is covered in 
      detail, with attention to XML DOM interfaces, XSLT processing, 
      SAX and more.</description>
   </book>
   <book id="bk112">
      <author>Galos, Mike</author>
      <title>Visual Studio 7: A Comprehensive Guide</title>
      <genre>Computer</genre>
      <price>49.95</price>
      <publish_date>2001-04-16</publish_date>
      <description>Microsoft Visual Studio 7 is explored in depth,
      looking at how Visual Basic, Visual C++, C#, and ASP+ are 
      integrated into a comprehensive development 
      environment.</description>
   </book>
</catalog>

你好

我一直在處理將xml文件分隔為名稱(元素和屬性)值對的方法,並遵循用戶定義的規則,該規則要求在語句中用逗號之前使用反斜杠。如果可以幫助我,我將非常高興。

提前致謝。

•如果值包含逗號,則需要在逗號前放置“ /”號

XSLT 1.0代碼:

<xsl:stylesheet exclude-result-prefixes="xs" version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="text"/>
<xsl:template match="/">
  <xsl:for-each select="//book/* | //book/@*">
     <xsl:variable name="value">
        <xsl:call-template name="string-replace-all">
           <xsl:with-param name="text" select="."/>
           <xsl:with-param name="replace" select="','"/>
           <xsl:with-param name="by" select="'\,'"/>
        </xsl:call-template>
     </xsl:variable>
     <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
     <xsl:value-of select="concat(local-name(.),',',$value)"/>
     <xsl:text disable-output-escaping="yes">></xsl:text>
     <xsl:text>&#10;</xsl:text>
  </xsl:for-each>
</xsl:template>
<xsl:template name="string-replace-all">
  <xsl:param name="text"/>
  <xsl:param name="replace"/>
  <xsl:param name="by"/>
  <xsl:choose>
     <xsl:when test="contains($text, $replace)">
        <xsl:value-of select="substring-before($text,$replace)"/>
        <xsl:value-of select="$by"/>
        <xsl:call-template name="string-replace-all">
           <xsl:with-param name="text" select="substring-after($text,$replace)"/>
           <xsl:with-param name="replace" select="$replace"/>
           <xsl:with-param name="by" select="$by"/>
        </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
        <xsl:value-of select="$text"/>
     </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
 </xsl:stylesheet>

注意:沒有邏輯可以分隔每個書本元素。 您可能需要檢查一下。

假設:

  1. 我們可以使用XSLT 2.0。
  2. 我們希望您在兩個地方都說反斜杠“ \\”,而不是您在一個地方說正斜杠“ /”。
  3. 我們也希望用反斜杠將右尖括號“>”轉義。 否則,如果文本中有“>”符號,則該行將以“>”結尾。

給定此XML和這些假設,可以將XSLT定義為:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    xmlns:p="processor"
    version="2.0">

    <!-- outputting to text, not xml, html, xhtml, etc. -->
    <xsl:output method="text" encoding="UTF-8" />

    <!-- suppress all text unless we specifically output it -->
    <xsl:template match="text()"/>

    <!-- match the document then all elements -->
    <xsl:template match="/">
        <xsl:apply-templates select="*"/>
    </xsl:template>

    <!-- match book elements differently than others: pull out the id attribute -->
    <xsl:template match="book">
        <xsl:value-of select="p:MakeLine(@id/name(), @id/.)"/>
        <xsl:apply-templates/>
    </xsl:template>

    <!-- match all elements inside a book -->
    <xsl:template match="book/*">
        <xsl:value-of select="p:MakeLine(name(), .)"/>
    </xsl:template>

    <!-- return a formatted line -->
    <xsl:function name="p:MakeLine">
        <xsl:param name="name" as="xs:string"/>
        <xsl:param name="body" as="xs:string"/>
        <xsl:value-of select="concat('&lt;', $name, ', ', p:Cleaned($body), '&gt;&#x0A;')"/>
    </xsl:function>

    <!-- cleanup and escape text: commas become \, and closing angle brackets become \> -->
    <xsl:function name="p:Cleaned">
        <xsl:param name="text" as="xs:string"/>
        <xsl:value-of select="normalize-space(replace(replace($text, ',', '\\,'), '&gt;', '\\&gt;'))"/>
    </xsl:function>
</xsl:stylesheet>

XML輸入:

<?xml version="1.0"?>
<catalog>
    <book id="bk101">
        <author>Gambardella, Matthew</author>
        <title>XML Developer's Guide</title>
        <genre>Computer</genre>
        <price>44.95</price>
        <publish_date>2000-10-01</publish_date>
        <description>An in-depth look at creating applications 
            with XML.</description>
    </book>
    <book id="bk102">
        <author>Ralls, Kim</author>
        <title>Midnight Rain</title>
        <genre>Fantasy</genre>
        <price>5.95</price>
        <publish_date>2000-12-16</publish_date>
        ...

成為文本輸出:

<id, bk101>
<author, Gambardella\, Matthew>
<title, XML Developer's Guide>
<genre, Computer>
<price, 44.95>
<publish_date, 2000-10-01>
<description, An in-depth look at creating applications with XML.>
<id, bk102>
<author, Ralls\, Kim>
<title, Midnight Rain>
<genre, Fantasy>
<price, 5.95>
<publish_date, 2000-12-16>
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM