使用XPath定位XML的某些部分的问题[重复]

时间:2023-02-09 17:01:08

Duplicate of

HOw to make the text repeat only once every for-each?

请问每个人每次只重复一次文本吗?

I have problem with using XPATH to point to some element in XML. Here is my XML code:

我有使用XPATH指向XML中的某些元素的问题。这是我的XML代码:

    <character>
      <name>some name </some name>
    </character>

  </quote>
 <quote>
  ....
  </quote>
 </quotes>

and here My XSLT code:

这里是我的XSLT代码:

<xsl:for-each select="quotes/quote">
    <xsl:value-of select="quotes/quote/character/name"/>
 </xsl:for-each>

WHat I try to do is that I try to list the name of character for every quote. BUt this code does not work WOuld you please help me with this problem? Thank you

我尝试做的是我尝试列出每个引号的字符名称。 BUt这段代码不起作用你请帮我解决这个问题吗?谢谢

2 个解决方案

#1


Inside the for-each, the context node is quote - so it looks like you just need:

在for-each中,上下文节点是引用 - 所以看起来你只需要:

<xsl:for-each select="quotes/quote">
    <xsl:value-of select="character/name"/>
</xsl:for-each>

Of course, the purists would argue that it would be better to use a match:

当然,纯粹主义者会争辩说使用比赛会更好:

<xsl:apply-templates select="quotes/quote" />
...
<xsl:template match="quote">
    <xsl:value-of select="character/name"/>
</xsl:template>

#2


The error is:
You use quotes/quote twice

错误是:您使用两次引号/引号

You might try this one:

你可以尝试这个:

<xsl:for-each select="quotes/quote">
  <xsl:value-of select="character/name"/>
</xsl:for-each>

#1


Inside the for-each, the context node is quote - so it looks like you just need:

在for-each中,上下文节点是引用 - 所以看起来你只需要:

<xsl:for-each select="quotes/quote">
    <xsl:value-of select="character/name"/>
</xsl:for-each>

Of course, the purists would argue that it would be better to use a match:

当然,纯粹主义者会争辩说使用比赛会更好:

<xsl:apply-templates select="quotes/quote" />
...
<xsl:template match="quote">
    <xsl:value-of select="character/name"/>
</xsl:template>

#2


The error is:
You use quotes/quote twice

错误是:您使用两次引号/引号

You might try this one:

你可以尝试这个:

<xsl:for-each select="quotes/quote">
  <xsl:value-of select="character/name"/>
</xsl:for-each>