xslt在$$$之后开始选择,但保留xml格式化

时间:2016-03-09 15:32:14

标签: xml xslt

我有以下结构......

<story><p>£££ Some Name $$$story text text text etc</p><p>more text text text etc</p></story>

我一直在尝试使用substring-after选择$$$之后的文本,并计划将<p>连接到开头。但是我使用这种方法丢失了所有标签。我正努力用正确的语法创建模板解决方案来做到这一点。 期望的输出 -

<story><p>story text text text etc</p><p>more text text text etc</p></story>

任何人都可以提供帮助。谢谢安吉拉

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">

    <xsl:output method="xml"  omit-xml-declaration="yes" indent="yes"/>

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

   <xsl:template match="VirtualComCMS">

    <xsl:for-each select="articles/article">
      <Article>
        <xsl:element name="StoryID">
          <xsl:value-of select="@id" />
        </xsl:element>
        <Name>
          <xsl:value-of select="Name"/>
        </Name>
        <ModificationDate>
          <xsl:value-of select="ModificationDate"/>
        </ModificationDate>
        <CreationDate>
          <xsl:value-of select="CreationDate"/>
        </CreationDate>
        <Headline>
          <xsl:value-of select="Headline"/>
        </Headline>
        <Subheadline>
          <xsl:value-of select="Subheadline"/>
        </Subheadline>
        <Story>
          <xsl:template match="@*|node()">
            <xsl:copy>
              <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
          </xsl:template>

          <xsl:template match="p/text()[contains(., '$$$')]">
            <xsl:value-of select="substring-after(., '$$$')"/>
          </xsl:template>
              <xsl:copy-of select="Body/div/node()"/>

        </Story>
        <Summary>
          <xsl:value-of select="Summary"/>
        </Summary>
        <Keywords>
          <xsl:value-of select="Keywords"/>
        </Keywords> 
        <Latitude>
          <xsl:value-of select="latitude"/>
        </Latitude>
        <Longitude>
          <xsl:value-of select="longitude"/>
        </Longitude>
        <Categories>
          <xsl:value-of select="Categories" />
        </Categories>
        <!-- <Categories>
            <xsl:value-of select="substring-before(substring-after(Categories, '/' ),'/')" />
         </Categories>
        <SubCategories>
          <xsl:value-of select="substring-after(substring-after(Categories, '/' ),'/')" />
        </SubCategories>-->
        <Priority>
          <xsl:value-of select="Priority"/>
        </Priority>
        <Byline>
          <!-- check story -->
          <xsl:choose>
            <xsl:when test="contains(Body/div/p, '$$$')">
              <xsl:value-of select="substring-after(substring-before(Body/div/p, '$$$'), '£££')" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="CreatedBy"/>
            </xsl:otherwise>
          </xsl:choose>
           </Byline>
        <Images>
          <xsl:for-each select="data">
            <xsl:element name="Image">
               <xsl:attribute name="id">
                      <xsl:value-of select="@id"/>
              </xsl:attribute>
               <!--<xsl:attribute name="fileext">
                      <xsl:value-of select="@ext"/>
              </xsl:attribute>-->
              <xsl:attribute name="filename">
                <xsl:value-of select="concat(@ImageToSaveID,'.jpg')"/>
              </xsl:attribute>
              <xsl:value-of select="urlpreview"/>
            </xsl:element>
          </xsl:for-each>
        </Images>
      </Article>
    </xsl:for-each>
 </xsl:template>




</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

我认为你的问题并不完全清楚,但是要让你开始:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="p/text()[contains(., '$$$')]">
    <xsl:value-of select="substring-after(., '$$$')"/>
</xsl:template>

</xsl:stylesheet>

应用于您的示例输入,结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<story>
  <p>story text text text etc</p>
  <p>more text text text etc</p>
</story>