0

我正在尝试从 XML 更改一些文本。然而它没有用。

我必须显示[intro]XML 中的所有文本,其中一些Path(text)应该在 display 之前更改[intro]

例如

<a href="3DD3D025-2236-49C9-A169-DD89A36DA0E6/eee.pdf"> --> wrong path

我想改为

<a href="Content\3\D\D\3DD3D025-2236-49C9-A169-DD89A36DA0E6/eee.pdf">

任何帮助将不胜感激。

示例 XML

<?xml version="1.0"?>
<root>
  <intro xml:lang="en">
    <div class="blueBar">
      <h2>Highlights</h2>
      <ul> 
        <li><a href="http://xxx/xxx/default.asp?lang=En">aaaa</a></li>
        <li><a href="http://xxx/default.asp?lang=En">bbbb</a></li>
        <li><a href="http://xxx/Content/1/C/D/1CD1DFC3-5149-4D61-A7C3-2D9CF7E65F8C/rrr.pdf">ccc</a></li>
        <li><a href="3DD3D025-2236-49C9-A169-DD89A36DA0E6/eee.pdf">pdf</a></li>
      </ul>
    </div>
  </intro>
  <intro> .....</intro>
</root>

示例 XSLT

<xsl:param name="language"/>

<xsl:template match="root">
  <xsl:for-each select="intro[lang($language)]//@href">
    <xsl:choose>
      <xsl:when test="contains(.,'pdf') and not(contains(.,'Content'))">
        <xsl:variable name="pdfGuid">
          <xsl:value-of select="substring(.,0,36)"/>
        </xsl:variable>
        <xsl:variable name="pdfPath">
          <xsl:value-of select="concat('&#47;','Content')"/>
          <xsl:value-of select="concat('&#47;', substring($pdfGuid, 1,1))"/>
          <xsl:value-of select="concat('&#47;', substring($pdfGuid, 2,1))"/>
          <xsl:value-of select="concat('&#47;', substring($pdfGuid, 3,1))"/>
          <xsl:value-of select="concat('&#47;', $pdfGuid)"/>
        </xsl:variable>       
        <xsl:value-of select="strJS:replace(string(.),string($pdfGuid),string($pdfPath))" disable-output-escaping="yes"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="." disable-output-escaping="yes"/>
      </xsl:otherwise>
    </xsl:choose>      
  </xsl:for-each>

  <div class="greenBox">
    <xsl:value-of select="intro[lang($language)]" disable-output-escaping="yes"/>
  </div>     

</xsl:template>
4

1 回答 1

0

当您在这里提出问题时,请清楚地说明您想要的输出以及您遇到的问题。

您对问题的描述与您的尝试之间存在差异,您在其中使用正斜杠,在 XSLT 中的路径开头使用斜杠,在描述中使用反斜杠。我假设你想要正斜杠,但这些很容易改变。我怀疑你想要的是这样的:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
  <xsl:param name="language" select="'en'" />

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

  <xsl:template match="/*">
    <div class="greenBox">
      <xsl:apply-templates select="intro[lang($language)]/node()" />
    </div>
  </xsl:template>

  <xsl:template match="@href[contains(., 'pdf') and not(contains(., 'Content'))]">
    <xsl:attribute name="href">
      <xsl:value-of select="concat('/Content/',
                                   substring(., 1, 1), '/',
                                   substring(., 2, 1), '/',
                                   substring(., 3, 1), '/',
                                   .)"/>
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

在您的示例输入上运行时,结果是:

<div class="greenBox">
    <div class="blueBar">
      <h2>Highlights</h2>
      <ul>
        <li>
          <a href="http://xxx/xxx/default.asp?lang=En">aaaa</a>
        </li>
        <li>
          <a href="http://xxx/default.asp?lang=En">bbbb</a>
        </li>
        <li>
          <a href="http://xxx/Content/1/C/D/1CD1DFC3-5149-4D61-A7C3-2D9CF7E65F8C/rrr.pdf">ccc</a>
        </li>
        <li>
          <a href="/Content/3/D/D/3DD3D025-2236-49C9-A169-DD89A36DA0E6/eee.pdf">pdf</a>
        </li>
      </ul>
    </div>
  </div>
于 2013-05-09T03:55:10.450 回答