Xsl 转换正确选择 children 但不是 indesign 中的祖先?

Xsl transforms correctly choosing children but not on ancestor in indesign?

我在使用 xsl 时遇到了一个奇怪的问题:

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

<xsl:template match="@value"> 
 <xsl:attribute name="value"> 
     <xsl:value-of select="translate(.,'&#xd;','&#xa;')"/> 
 </xsl:attribute> 
</xsl:template> 

<xsl:template match="text">
    <xsl:copy>
        <xsl:attribute name="value">
            <xsl:apply-templates/>
        </xsl:attribute>
    </xsl:copy>
</xsl:template>


<xsl:template match="b">
    <xsl:text>[b]</xsl:text>
        <xsl:apply-templates/>
    <xsl:text>[/b]</xsl:text>
</xsl:template>


<xsl:template match="i">
    <xsl:text>[i]</xsl:text>
        <xsl:apply-templates/>
    <xsl:text>[/i]</xsl:text>
</xsl:template>
<xsl:template match="@href"/>

我的 Xml 看起来像这样:

<medium>
     <sheets>
          <sheet>
               <site>
                    All Elements
               </site>
          </sheet>
     </sheets>
</medium>

<text-area> 标签将如下所示:

<text-area>
     <text>
          this is the <b>normal</b> indesign content
     </text>
</text-area>.

xsl 之后:

<text-area>
     <text value="this is the [b]normal[/b] indesign content"/>
</text-area>

现在的问题:

当我在 indesign 中将其导出并选择 sheets、sheet、站点或任何其他 <text-area> 是 child 的元素时,它会将其导出为:

<text-area>
     <text value="this is the [b]normal[/b] indesign content"/>
</text-area>

只有当我选择 medium 时,它才会看起来像这样:

<text-area>
     <text value="this is the normal indesign content">
          this is the <b>normal</b> indesign content
     </text>
</text-area>

为什么会这样?我不知道......为什么它在 "medium" 以外的所有其他元素上工作?

完成xml例子:

<medium xmlns="example" xmlns:xsi="example" xsi:schemaLocation="example" name="Karte" description="asd" summary="asd" id="Karte_3/11/2015/16">
  <thumbnail src="http://karte/thumb.png"/>
  <sales-plan>
    <download price="3"/>
  </sales-plan>
  <sheets>
    <sheet>
      <site width="216.0" height="111.0" trim="3">
        <text-area x="116.00000007" y="32.10000002" width="88.00" height="51.60" line-height="8.4666666720" readonly="true" text-align="left" orientation="0">
          <text>
            <b>Ein herzlich frohes Weihnachtsfest, viel Freude, Glück und Gesundheit für das Neue Jahr.</b>
          </text>
          <font size="7.0555555600"/>
          <foreground-color c="0" m="0" y="0" k="0"/>
        </text-area>
        <graphic-area x="-0.00000000" y="98.30000006" width="216.00" height="12.70" orientation="0">
          <foreground-color-ref idref="user-color"/>
          <svg>
            <g class="user-color" style="fill:[user-color];">
              <rect x="0" y="0" width="100%" height="100%" style="fill:[user-color];"/>
              <rect x="0" y="0" width="100%" height="100%" fill=" #000" style="fill-opacity:0"/>
            </g>
          </svg>
        </graphic-area>
        <text-area x="10.20000000" y="102.00416673" width="202.800000" height="5.995833" line-height="4.2333333360" readonly="true" text-align="left" orientation="0">
          <text value="[user-firmname] · [user-firstname] [user-lastname] · [user-street] [user-housenr] · [user-zipcode] [user-city]">Firmenname · Ansprechpartner · Straße H.Nr. · PLZ · Ort</text>
          <font size="3.5277777800" weight="normal"/>
          <foreground-color c="0" m="0" y="0" k="0"/>
        </text-area>
      </site>
    </sheet>
  </sheets>
</medium>

提前致谢!

medium 元素声明了一个您需要考虑的 default 命名空间。如果您选择从 medium 元素导出,则此默认命名空间适用于 medium 元素及其所有后代 - 包括 textb 元素。

现在,匹配

的模板
<xsl:template match="b">

仅匹配 no 命名空间中的元素。要查找默认命名空间中的 b 元素,您需要在样式表中 重新声明 此命名空间,并在模板匹配中为元素名称添加前缀。

样式表

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:ex="example">
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

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

<xsl:template match="@value"> 
 <xsl:attribute name="value"> 
     <xsl:value-of select="translate(.,'&#xd;','&#xa;')"/> 
 </xsl:attribute> 
</xsl:template> 

<xsl:template match="ex:text">
    <xsl:copy>
        <xsl:attribute name="value">
            <xsl:apply-templates/>
        </xsl:attribute>
    </xsl:copy>
</xsl:template>


<xsl:template match="ex:b">
    <xsl:text>[b]</xsl:text>
        <xsl:apply-templates/>
    <xsl:text>[/b]</xsl:text>
</xsl:template>


<xsl:template match="ex:i">
    <xsl:text>[i]</xsl:text>
        <xsl:apply-templates/>
    <xsl:text>[/i]</xsl:text>
</xsl:template>
<xsl:template match="@href"/>
</xsl:transform>

XML输出

只有重要的部分:

<text value="&#xA;            [b]Ein herzlich frohes Weihnachtsfest, viel Freude, Glück und Gesundheit für das Neue Jahr.[/b]&#xA;          "/>

顺便说一下,如果您希望样式表在输入 XML 中不考虑名称空间的情况下工作,请使用以下样式表。我还简化了匹配 bi 元素的模板,因为这两个模板的作用基本相同。

您确定要将模板应用到 *[not(self::table)] 吗?将模板应用于身份模板中的所有内容并编写另一个模板会更清晰

<xsl:template match="*[local-name() = 'table']"/>

防止输出table个元素。

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

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

    <xsl:template match="@value"> 
     <xsl:attribute name="value"> 
         <xsl:value-of select="translate(.,'&#xd;','&#xa;')"/> 
     </xsl:attribute> 
    </xsl:template> 

    <xsl:template match="*[local-name() = 'text']">
        <xsl:copy>
            <xsl:attribute name="value">
                <xsl:apply-templates/>
            </xsl:attribute>
        </xsl:copy>
    </xsl:template>


    <xsl:template match="*[local-name() = 'b' or local-name() = 'i']">
        <xsl:value-of select="concat('[',local-name(),']')"/>
            <xsl:apply-templates/>
        <xsl:value-of select="concat('[/',local-name(),']')"/>
    </xsl:template>

    <xsl:template match="@href"/>

</xsl:transform>