Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用XSL将XML文件划分为名称-值对 马修·甘巴德拉 XML开发人员指南 电脑类 44.95 2000-10-01 深入了解如何创建应用程序 使用XML。 拉尔斯,金 夜雨 幻想 5.95 2000-12-16 一位前建筑师与企业僵尸搏斗, 一个邪恶的女巫,和她自己的童年成为女王 世界的一部分。 科雷茨,伊娃 梅夫上升 幻想 5.95 2000-11-17 在纳米技术崩溃之后 在英国社会中,年轻的幸存者奠定了基础 建立新社会的基础。 科雷茨,伊娃 奥伯伦的遗产 幻想 5.95 2001-03-10 在后启示录时代的英格兰,神秘的 只被称为Oberon的代理帮助创造新的生命 为了伦敦的居民。梅夫续集 向上的 科雷茨,伊娃 破碎的圣杯 幻想 5.95 2001-09-10 梅芙的两个女儿,同父异母的姐妹, 为争夺英国的控制权而互相争斗。续集 奥伯伦的遗产。 兰德尔,辛西娅 情人鸟 浪漫 4.95 2000-09-02 当卡拉在鸟类学上遇到保罗时 在会议上,当羽毛被激怒时,脾气就会暴跳如雷。 瑟曼,保拉 水花四溅 浪漫 4.95 2000-11-02 一个深海潜水员二十岁就能找到真爱 海底千里。 克诺尔,斯特凡 令人毛骨悚然的爬虫 恐怖 4.95 2000-12-06 关于蟑螂的恐怖故事选集, 蜈蚣、蝎子和其他昆虫。 克雷斯,彼得 悖论丢失 科幻小说 6.95 2000-11-02 经过一次无意的海森堡之旅 不确定性装置,詹姆斯·萨尔韦发现了问题 是量子的。 蒂姆·奥布莱恩 微软.NET:编程圣经 电脑类 36.95 2000-12-09 微软的.NET计划在 详细信息请参阅本深度程序员参考资料。 蒂姆·奥布莱恩 MSXML3:综合指南 电脑类 36.95 2000-12-01 中介绍了Microsoft MSXML3解析器 详细信息,注意XML DOM接口、XSLT处理、, 萨克斯和更多。 加洛斯,迈克 VisualStudio7:综合指南 电脑类 49.95 2001-04-16 深入探索了Microsoft Visual Studio 7, 看看Visual Basic、Visual C++、C语言和ASP+ 综合开发 环境_Xml_Xslt - Fatal编程技术网

使用XSL将XML文件划分为名称-值对 马修·甘巴德拉 XML开发人员指南 电脑类 44.95 2000-10-01 深入了解如何创建应用程序 使用XML。 拉尔斯,金 夜雨 幻想 5.95 2000-12-16 一位前建筑师与企业僵尸搏斗, 一个邪恶的女巫,和她自己的童年成为女王 世界的一部分。 科雷茨,伊娃 梅夫上升 幻想 5.95 2000-11-17 在纳米技术崩溃之后 在英国社会中,年轻的幸存者奠定了基础 建立新社会的基础。 科雷茨,伊娃 奥伯伦的遗产 幻想 5.95 2001-03-10 在后启示录时代的英格兰,神秘的 只被称为Oberon的代理帮助创造新的生命 为了伦敦的居民。梅夫续集 向上的 科雷茨,伊娃 破碎的圣杯 幻想 5.95 2001-09-10 梅芙的两个女儿,同父异母的姐妹, 为争夺英国的控制权而互相争斗。续集 奥伯伦的遗产。 兰德尔,辛西娅 情人鸟 浪漫 4.95 2000-09-02 当卡拉在鸟类学上遇到保罗时 在会议上,当羽毛被激怒时,脾气就会暴跳如雷。 瑟曼,保拉 水花四溅 浪漫 4.95 2000-11-02 一个深海潜水员二十岁就能找到真爱 海底千里。 克诺尔,斯特凡 令人毛骨悚然的爬虫 恐怖 4.95 2000-12-06 关于蟑螂的恐怖故事选集, 蜈蚣、蝎子和其他昆虫。 克雷斯,彼得 悖论丢失 科幻小说 6.95 2000-11-02 经过一次无意的海森堡之旅 不确定性装置,詹姆斯·萨尔韦发现了问题 是量子的。 蒂姆·奥布莱恩 微软.NET:编程圣经 电脑类 36.95 2000-12-09 微软的.NET计划在 详细信息请参阅本深度程序员参考资料。 蒂姆·奥布莱恩 MSXML3:综合指南 电脑类 36.95 2000-12-01 中介绍了Microsoft MSXML3解析器 详细信息,注意XML DOM接口、XSLT处理、, 萨克斯和更多。 加洛斯,迈克 VisualStudio7:综合指南 电脑类 49.95 2001-04-16 深入探索了Microsoft Visual Studio 7, 看看Visual Basic、Visual C++、C语言和ASP+ 综合开发 环境

使用XSL将XML文件划分为名称-值对 马修·甘巴德拉 XML开发人员指南 电脑类 44.95 2000-10-01 深入了解如何创建应用程序 使用XML。 拉尔斯,金 夜雨 幻想 5.95 2000-12-16 一位前建筑师与企业僵尸搏斗, 一个邪恶的女巫,和她自己的童年成为女王 世界的一部分。 科雷茨,伊娃 梅夫上升 幻想 5.95 2000-11-17 在纳米技术崩溃之后 在英国社会中,年轻的幸存者奠定了基础 建立新社会的基础。 科雷茨,伊娃 奥伯伦的遗产 幻想 5.95 2001-03-10 在后启示录时代的英格兰,神秘的 只被称为Oberon的代理帮助创造新的生命 为了伦敦的居民。梅夫续集 向上的 科雷茨,伊娃 破碎的圣杯 幻想 5.95 2001-09-10 梅芙的两个女儿,同父异母的姐妹, 为争夺英国的控制权而互相争斗。续集 奥伯伦的遗产。 兰德尔,辛西娅 情人鸟 浪漫 4.95 2000-09-02 当卡拉在鸟类学上遇到保罗时 在会议上,当羽毛被激怒时,脾气就会暴跳如雷。 瑟曼,保拉 水花四溅 浪漫 4.95 2000-11-02 一个深海潜水员二十岁就能找到真爱 海底千里。 克诺尔,斯特凡 令人毛骨悚然的爬虫 恐怖 4.95 2000-12-06 关于蟑螂的恐怖故事选集, 蜈蚣、蝎子和其他昆虫。 克雷斯,彼得 悖论丢失 科幻小说 6.95 2000-11-02 经过一次无意的海森堡之旅 不确定性装置,詹姆斯·萨尔韦发现了问题 是量子的。 蒂姆·奥布莱恩 微软.NET:编程圣经 电脑类 36.95 2000-12-09 微软的.NET计划在 详细信息请参阅本深度程序员参考资料。 蒂姆·奥布莱恩 MSXML3:综合指南 电脑类 36.95 2000-12-01 中介绍了Microsoft MSXML3解析器 详细信息,注意XML DOM接口、XSLT处理、, 萨克斯和更多。 加洛斯,迈克 VisualStudio7:综合指南 电脑类 49.95 2001-04-16 深入探索了Microsoft Visual Studio 7, 看看Visual Basic、Visual C++、C语言和ASP+ 综合开发 环境,xml,xslt,Xml,Xslt,嗨 我一直在处理将xml文件分离为名称(元素和属性)-值对的问题,下面是一个用户定义的规则,该规则要求在语句中逗号之前使用反斜杠。如果您能帮助我,我将非常高兴 提前谢谢 •如果值包含逗号而非“/”符号,则需要将其置于逗号之前XSLT 1.0代码: <?xml version="1.0"?> <catalog> <book id="bk101"> <author>Gambardella, Matthew</author>

我一直在处理将xml文件分离为名称(元素和属性)-值对的问题,下面是一个用户定义的规则,该规则要求在语句中逗号之前使用反斜杠。如果您能帮助我,我将非常高兴

提前谢谢


•如果值包含逗号而非“/”符号,则需要将其置于逗号之前

XSLT 1.0代码:

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

>


注意:没有提供用于分隔每个book元素的逻辑。你可能需要检查一下这个

假设:

  • 我们可以使用XSLT2.0
  • 我们想要反斜杠“\”,就像你在两个地方说的,而不是正斜杠“/”,就像你在一个地方说的
  • 我们需要右尖括号“>”,并用反斜杠转义。否则,如果文本中有'>'符号,该行将过早以'>'结束
  • 鉴于此XML和这些假设,XSLT可以定义为:

    <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>
    
    
    
    XML输入:

    <?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开发人员指南
    电脑类
    44.95
    2000-10-01
    深入了解如何创建应用程序
    使用XML。
    拉尔斯,金
    夜雨
    幻想
    5.95
    2000-12-16
    ...
    
    成为文本输出:

    <?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>
            ...
    
    
    
    您能使用XSLT2.0吗?我已经将您的问题回滚到其原始形式。否则,已经给出的答案将毫无意义。
    <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>
    ...