如何从XML获取特定标记以使用XSLT将其放入HTML中?

时间:2014-06-17 10:24:52

标签: xslt

我需要转换XML,以获得“无花果”。来自XML的标记,它放在相应的交叉引用上。

输入:

<floats>
  <fig id=”fig1”>
    <p>First figure</p>
    <link locator=”fig1.tif”/>
  </fig>
  <fig id=”fig2”>
    <p>Second figure</p>
    <link locator=”fig2.tif”/>
  </fig>
</floats>
<body>
  <p>Paragraph 1<cross-ref refid=”fig1”>Fig. 1</cross-ref><float-anchor refid="fig1" /></p>
  <p>Paragraph 2<cross-ref refid=”fig2”>Fig. 2</cross-ref><float-anchor refid="fig2" /></p>
</body>

输出:

<p class=”txt”>Paragraph 1<a href=”#fig1”>Fig. 1</a></p>
<div class=”figure” id=”fig1”>
  <p class=”fig”>First figure</p>
  <img src=”fig1.tif”/>
</div>
<p class=”txt”>Paragraph 2<a href=”#fig2”>Fig. 2</a></p>
<div class=”figure” id=”fig2”>
  <p class=”fig”>Second figure</p>
  <img src=”fig2.tif”/>
</div>

你能否就此提出建议。提前谢谢。

2 个答案:

答案 0 :(得分:0)

尝试以下转换是否符合您的需求。它尚未考虑Joel M. Lamsen关于cross-reffloat-anchor的评论。 更新:现在确实如此。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
    <xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
</xsl:template>

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

<xsl:template match="p">
    <xsl:copy>
        <xsl:attribute name="class">text</xsl:attribute>
        <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
    <xsl:apply-templates select="float-anchor" mode="float" />
</xsl:template>

<xsl:template match="cross-ref">
    <xsl:variable name="refid" select="@refid" />
    <xsl:element name="a">
        <xsl:attribute name="href">
            <xsl:value-of select="concat('#', $refid)" />
        </xsl:attribute>
        <xsl:value-of select="." />
    </xsl:element>
</xsl:template>

<xsl:template match="float-anchor" />

<xsl:template match="float-anchor" mode="float">
    <xsl:variable name="refid" select="@refid" />
    <xsl:element name="div">
        <xsl:attribute name="id"><xsl:value-of select="$refid" /></xsl:attribute>
        <xsl:attribute name="class">figure</xsl:attribute>
        <xsl:apply-templates select="//floats/fig[@id=$refid]/*" mode="float" />
    </xsl:element>
</xsl:template>

<xsl:template match="p" mode="float">
    <xsl:copy>
        <xsl:attribute name="class">fig</xsl:attribute>
        <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
</xsl:template>

<xsl:template match="link" mode="float">
    <xsl:element name="img">
        <xsl:attribute name="src"><xsl:value-of select="@locator" /></xsl:attribute>
    </xsl:element>
</xsl:template>
</xsl:transform>

答案 1 :(得分:0)

尝试以下样式表:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:strip-space elements="*"/>

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

    <xsl:variable name="Fig" select="root/floats"/>

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

    <xsl:template match="p">
        <p class="txt">
            <xsl:apply-templates select="@*|node()"/>
        </p>
        <xsl:if test="float-anchor">
            <xsl:for-each select="float-anchor">
                <xsl:apply-templates select="$Fig/fig[@id=current()/@refid]" mode="transfer"/>
            </xsl:for-each>
        </xsl:if>
    </xsl:template>

    <xsl:template match="floats|float-anchor"/>

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

    <xsl:template match="fig" mode="transfer">
        <div class="figure" id="{@id}">
            <xsl:apply-templates/>
        </div>
    </xsl:template>

    <xsl:template match="link">
        <img src="{@locator}"/>
    </xsl:template>

    <xsl:template match="cross-ref">
        <a href="#{@refid}">
            <xsl:apply-templates/>
        </a>
    </xsl:template>

</xsl:stylesheet>

由于输入XML无效(缺少根节点),请考虑以下输入XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <floats>
        <fig id="fig1">
            <p>First figure</p>
            <link locator="fig1.tif"/>
        </fig>
        <fig id="fig2">
            <p>Second figure</p>
            <link locator="fig2.tif"/>
        </fig>
    </floats>
    <body>
        <p>Paragraph 1<cross-ref refid="fig1">Fig. 1</cross-ref><float-anchor refid="fig1" /></p>
        <p>Paragraph 2<cross-ref refid="fig2">Fig. 2</cross-ref><float-anchor refid="fig2" /></p>
    </body>
</root>

当应用于上面的样式表时,输出为:

<p class="txt">Paragraph 1<a href="#fig1">Fig. 1</a>
</p>
<div class="figure" id="fig1">
    <p class="txt">First figure</p>
    <img src="fig1.tif"/>
</div>
<p class="txt">Paragraph 2<a href="#fig2">Fig. 2</a>
</p>
<div class="figure" id="fig2">
    <p class="txt">Second figure</p>
    <img src="fig2.tif"/>
</div>