通过使用XSL将XML文件划分为名称 - 值对

时间:2014-07-28 18:38:09

标签: xml xslt

<?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文件分隔为名称(元素和属性)-value对,如下所示,用户定义的规则需要在语句中使用逗号之前使用反斜杠。如果你能帮助我,我将会是非常高兴。

先谢谢。

•如果值包含逗号而不是&#34; /&#34;标志需要放在逗号之前

2 个答案:

答案 0 :(得分:0)

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 :(得分:0)

假设:

  1. 我们可以使用XSLT 2.0。
  2. 我们想要反斜杠,'\',正如你在两个地方所说,而不是正斜杠,'/',正如你在一个地方所说的那样。
  3. 我们希望使用反斜杠转义关闭尖括号“&gt;”。否则,该行将以'&gt;'结尾太早,如果有'&gt;'文本中的符号。
  4. 鉴于此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>
    ...