Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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
在XSLT期间保留某些html标记_Html_Xml_Xslt - Fatal编程技术网

在XSLT期间保留某些html标记

在XSLT期间保留某些html标记,html,xml,xslt,Html,Xml,Xslt,我已经找到了stackflow的解决方案,但似乎没有一个适合我。这是我的问题。假设我有以下文本: 资料来源: <greatgrandparent> <grandparent> <parent> <sibling> Hey, im the sibling . </sibling> <description> $300$ &

我已经找到了stackflow的解决方案,但似乎没有一个适合我。这是我的问题。假设我有以下文本:

资料来源:

<greatgrandparent>
<grandparent>
    <parent>
         <sibling>
              Hey, im the sibling .
          </sibling>
        <description>
        $300$ <br/> $250 <br/> $200! <br/> <p> Yes, that is right! <br/> You can own a ps3 for only $200 </p>
        </description>
    </parent>
    <parent>
         ... (SAME FORMAT)
    </parent>
       ... (Several more parents)
</grandparent>
</greatgrandparent>

嘿,我是兄弟姐妹。
300美元250美元200美元<是的,没错
您只需200美元即可拥有一台ps3

... (格式相同) ... (还有几位家长)
输出:

 <newprice>
        $300$ <br/> $250 <br/> $200! <br/> Yes, that is right! <br/> You can own a ps3 for only $200  
    </newprice>

300美元250美元200美元
是的,没错
您只需200美元即可拥有一台ps3
我似乎找不到一个办法来做那件事

当前XSL:

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

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

    <xsl:template match = "grandparent">

    <xsl:for-each select = "parent" >
          <newprice>
             <xsl:apply-templates>
           </newprice>
    </xsl:for-each>
    </xsl:template> 

<xsl:template match="description"> 
    <xsl:element name="newprice"> 
       <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 

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

的内容不应该在
CDATA
元素中吗?然后可能会禁用xsl:value of..

的输出编码。的内容不应该在
CDATA
元素中吗?然后可能会禁用xsl:value of..

上的输出编码,您应该查看一下

你可能会得到这样的结果:

<xsl:template match="description">
    <xsl:copy-of select="."/>
</xsl:template>

您应该查看

你可能会得到这样的结果:

<xsl:template match="description">
    <xsl:copy-of select="."/>
</xsl:template>

使用模板定义特定元素的行为

<!-- after standard identity template -->

<xsl:template match="description">
    <xsl:element name="newprice">
       <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

<xsl:template match="p">
   <xsl:apply-templates/>
</xsl:template>
因此,我们的解决方案大致如下:

<xsl:stylesheet 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
    exclude-result-prefixes="xs">

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

<!-- this is the identity template -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="description">
    <xsl:element name="newprice">
       <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

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

</xsl:stylesheet>

使用模板定义特定元素的行为

<!-- after standard identity template -->

<xsl:template match="description">
    <xsl:element name="newprice">
       <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

<xsl:template match="p">
   <xsl:apply-templates/>
</xsl:template>
因此,我们的解决方案大致如下:

<xsl:stylesheet 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
    exclude-result-prefixes="xs">

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

<!-- this is the identity template -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="description">
    <xsl:element name="newprice">
       <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

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

</xsl:stylesheet>

最短的解决方案可能是这个:

<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="description">
   <newprice>
     <xsl:copy-of select="node()"/>
   </newprice>
 </xsl:template>

 <xsl:template match="text()[not(ancestor::description)]"/>
</xsl:stylesheet>

将此转换应用于提供的XML文档时,将生成所需的结果:

<newprice>
        $300$ <br /> $250 <br /> $200! <br /> <p> Yes, that is right! <br /> You can own a ps3 for only $200 </p>
        </newprice>

300美元250美元200美元<是的,没错
您只需200美元即可拥有一台ps3

注意事项

<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="description">
   <newprice>
     <xsl:copy-of select="node()"/>
   </newprice>
 </xsl:template>

 <xsl:template match="text()[not(ancestor::description)]"/>
</xsl:stylesheet>
  • 使用
    复制以
    说明
    为根的所有子树,而不复制根本身

  • 我们如何覆盖(使用特定的空模板)XSLT内置模板,防止输出任何不是
    元素后代的文本节点


  • 最短的解决方案可能是这个

    <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="description">
       <newprice>
         <xsl:copy-of select="node()"/>
       </newprice>
     </xsl:template>
    
     <xsl:template match="text()[not(ancestor::description)]"/>
    </xsl:stylesheet>
    
    
    
    将此转换应用于提供的XML文档时,将生成所需的结果:

    <newprice>
            $300$ <br /> $250 <br /> $200! <br /> <p> Yes, that is right! <br /> You can own a ps3 for only $200 </p>
            </newprice>
    
    
    300美元250美元200美元<是的,没错
    您只需200美元即可拥有一台ps3

    注意事项

    <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="description">
       <newprice>
         <xsl:copy-of select="node()"/>
       </newprice>
     </xsl:template>
    
     <xsl:template match="text()[not(ancestor::description)]"/>
    </xsl:stylesheet>
    
  • 使用
    复制以
    说明
    为根的所有子树,而不复制根本身

  • 我们如何覆盖(使用特定的空模板)XSLT内置模板,防止输出任何不是
    元素后代的文本节点


  • 我要试试这个。我会告诉你事情的进展。我想我遇到了一个问题,是因为一些奇怪的原因,在所有这些中添加了其他字段。谢谢你的回复!使用标识模板是
    的关键。如果您需要完整的示例或这是否足够,请告诉我。ehh。我不能让它工作。它似乎在添加描述的所有同级。给定的代码适用于您提供的示例。还有什么我们需要知道的吗?我要编辑一下,等一下。非常感谢你。需要尽快完成这项工作,六月份有一个现场演示。我将尝试一下。我会告诉你事情的进展。我想我遇到了一个问题,是因为一些奇怪的原因,在所有这些中添加了其他字段。谢谢你的回复!使用标识模板是
    的关键。如果您需要完整的示例或这是否足够,请告诉我。ehh。我不能让它工作。它似乎在添加描述的所有同级。给定的代码适用于您提供的示例。还有什么我们需要知道的吗?我要编辑一下,等一下。非常感谢你。需要尽快完成这项工作,六月份得到了现场演示。感谢您的回复!我的主管没有告诉我他们需要成为CDTATA的一部分。谢谢你的回复!我的主管没有告诉我他们需要成为CDTATA的一部分。